Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make it so you don't have to hold the key to sprint in this script?

This current script makes it so when you HOLD down the shift key you sprint, however I'd like to make it so when you press the key you can sprint but you don't need to hold the key, and then once you press the key again, you stop sprinting? How would I do this? Your help would be greatly appreciated! :)

wait()local Player=game.Players.LocalPlayer local Mouse = Player:GetMouse() local Speed = 33 local Humanoid = Player.Character:WaitForChild("Humanoid") local OrigSpeed = Humanoid.WalkSpeed

Mouse.KeyDown:connect(function(Key)local Code=Key:byte()if(Code==48)then Humanoid.WalkSpeed = Speed end end) Mouse.KeyUp:connect(function(Key)local Code=Key:byte()if(Code==48)then Humanoid.WalkSpeed = OrigSpeed end end)

1 answer

I set the button to activate it as shift but you can change that to whatever you want. Don't use KeyDown anymore, it's deprecated, UserInputService is more efficient.

local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
Player.CharacterAdded:wait()

local Mouse = Player:GetMouse()
local Speed = 33
local Humanoid = Player.Character:WaitForChild("Humanoid")
local OrigSpeed = Humanoid.WalkSpeed
local active = false

UserInputService.InputBegan:Connect(function(input)
local key = input.KeyCode
	if key == Enum.KeyCode.LeftShift and active == false then
		Humanoid.WalkSpeed = Speed
		active = true
	elseif key == Enum.KeyCode.LeftShift and active == true then
		Humanoid.WalkSpeed = OrigSpeed
		active = false
	end
end) 

This discussion is preserved as it was. New answers, comments, and votes are closed.