Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I add a sprinting feature?

Asked by 3 years ago

I want to add a sprinting feature in my game so that when shift is pressed the playerspeed increases

2 answers

Log in to vote
1
Answered by 3 years ago

Here you go:

Create a LocalScript inside of StarterCharacterScripts located under StarterPlayer and insert the script below:

local plr = game:GetService('Players').LocalPlayer
local humanoid = plr.Character:WaitForChild('Humanoid')
local UIS = game:GetService('UserInputService')
local defaultSpeed = humanoid.WalkSpeed --[[ Gets the default walkspeed]]

UIS.InputBegan:Connect(function(input) --[[ Runs when any input is detected]]
    if input.KeyCode == Enum.KeyCode.LeftShift then --[[ If the input is the left shift key]]
        humanoid.WalkSpeed += 16 --[[ Increases WalkSpeed. Change this to your needs.]]
    end
end)

UIS.InputEnded:Connect(function(input) --[[ Runs when any input is ended (Like a keyUp thing)]]
    if input.KeyCode == Enum.KeyCode.LeftShift then --[[ Again detects if it's the left shift key]]
        humanoid.WalkSpeed = defaultSpeed --[[ Sets your speed back to the default WalkSpeed (16 by default whenever you create a game)]]
    end
end)
Ad
Log in to vote
0
Answered by 3 years ago

We can use UserInputService and make it so when you hold shift down it sets your walkspeed to 20, and when it is released it is set back to 16.

Answer this question