I want to add a sprinting feature in my game so that when shift is pressed the playerspeed increases
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)
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.