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

Activate when button is pressed down?

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.

1 comment

0

Please use a code block to hold your scripts

1 answer

✓ Accepted answer

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)

4 comments

0

Why you no anonymous functions~

0

Because I just copy pasted his code. I don't want to confuse him.

0

Thank you so much

0

If this did help you, please accept it

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