Everytime the player press Q, it increases their WalkSpeed until it reaches 250. But if I stop pressing Q, my speed keeps going up. The same thing happens for my second function. It makes my WalkSpeed go down until it reaches 20 but if I stop in the middle of it lets say, it'll keep going down. How can I check when the player stops pressing the key so the function can terminate?
local player = game.Players.LocalPlayer
local char = player.Character
KeyHeld = false
KeyHeld2 = false
--When the user presses Q, it'll keep increasing their speed until it reaches 250, so how would I make it so it stops when the user stops pressing Q?
function onKeyPress(inputObject,gameProcessed)
if inputObject.KeyCode == Enum.KeyCode.Q and script.Active.Value == true then
KeyHeld = true
KeyHeld2 = false
while KeyHeld do
if char.Humanoid.WalkSpeed < 250 and char.Humanoid.WalkSpeed >= 20 then
char.Humanoid.WalkSpeed = char.Humanoid.WalkSpeed + 10
end
wait()
end
end
end
--Same as above for here but it'll keep going down until 20
function onKeyPress2(inputObject,gameProcessed)
if inputObject.KeyCode == Enum.KeyCode.E and script.Active.Value == true then
KeyHeld = false
KeyHeld2 = true
while KeyHeld2 do
if char.Humanoid.WalkSpeed <= 250 and char.Humanoid.WalkSpeed >= 30 then
char.Humanoid.WalkSpeed = char.Humanoid.WalkSpeed - 10
end
wait()
end
end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)
game:GetService("UserInputService").InputBegan:connect(onKeyPress2)

1 comment
you need to make four functions, two more for .InputEnded