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

Activate when button is pressed down?

Asked by 6 years ago

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.

0
Please use a code block to hold your scripts lukeb50 631 — 6y

1 answer

Log in to vote
1
Answered by
lukeb50 631 Moderation Voter
6 years ago
Edited 6 years ago

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)
0
Why you no anonymous functions~ Azarth 3141 — 6y
0
Because I just copy pasted his code. I don't want to confuse him. lukeb50 631 — 6y
0
Thank you so much superke12 -1 — 6y
0
If this did help you, please accept it lukeb50 631 — 6y
Ad

Answer this question