I want to make a running script that activates while the left shift button is pressed down, but i only know how to make it activate when it is pressed once.
function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.LeftShift then game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 24 end end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)
ive tried changing the onKeyPress to onKeyDown in an attempt to fix it but i had no luck.
onKeyPress
is only the name of the function, it has no impact on what it does.
What you want to change is the last line, which connects the InputBegan
event of UserInputService
to the function. To reset the player's walkspeed when they release the LeftShift
, you want to use the InputEnded
event like so.
function keyUp(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.LeftShift then game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16 end end game:GetService("UserInputService").InputEnded:connect(keyUp)
This is a new function that resets your speed to 16 when you let go of the shift key. Your final code would look like so:
function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.LeftShift then game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 24 end end game:GetService("UserInputService").InputBegan:connect(onKeyPress) function keyUp(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.LeftShift then game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16 end end game:GetService("UserInputService").InputEnded:connect(keyUp)