Skinstashery
Roblox
Home / Roblox / Studio script library / Sprint with Shift
RB-004

Roblox sprint with shift script

Hold Left Shift to run faster. Let go to walk again.

Script type
LocalScript
Where it goes
StarterPlayer > StarterPlayerScripts
Category
Movement

For your own game in Roblox Studio. Not for use in other people's games.

The code

-- Sprint: hold the key to run faster. This is a LocalScript (it runs on the player's own device).

local WALK_SPEED = 16
local SPRINT_SPEED = 28
local SPRINT_KEY = Enum.KeyCode.LeftShift

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer

local function getHumanoid()
	local character = player.Character
	return character and character:FindFirstChildOfClass("Humanoid")
end

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then
		return -- the player is typing in chat or using a menu
	end
	if input.KeyCode == SPRINT_KEY then
		local humanoid = getHumanoid()
		if humanoid then
			humanoid.WalkSpeed = SPRINT_SPEED
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == SPRINT_KEY then
		local humanoid = getHumanoid()
		if humanoid then
			humanoid.WalkSpeed = WALK_SPEED
		end
	end
end)

How to set it up

  1. In the Explorer, open StarterPlayer, then StarterPlayerScripts.
  2. Click the plus button next to StarterPlayerScripts and add a LocalScript.
  3. Paste the code into the LocalScript.
  4. Play and hold Left Shift while walking.

Change it

More in Movement

All Roblox scripts

More Roblox tools