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

Stopping a function after a key is finished pressing?

Asked by 6 years ago

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)
0
you need to make four functions, two more for .InputEnded abnotaddable 920 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

If you change it such that the InputBegan functions are given to a variable, then you can disconnect the function, therefore stopping it. Idk how to explain but like this

e.g

a = game:GetService("UserInputService").InputBegan:connect(onKeyPress)
b = game:GetService("UserInputService").InputBegan:connect(onKeyPress2)

Therefore when you don't want the function to run anymore, you can do either:

a:Disconnect()

or the other depending on which one you want to stop

Ad
Log in to vote
0
Answered by 6 years ago

something like this? idk im using click detectors for ease it prob work the same for yours

local detector = script.Parent:WaitForChild('ClickDetector')
local debounce = false

detector.MouseClick:connect(function()
    if debounce == false then
        debounce = true
        print('how about no')
        elseif debounce == true then
        debounce = false
        print('kid friendly word')
    end
end)

your rough example would be:

local plyr = game:GetService('Players').LocalPlayer
local mouse = plyr:GetMouse()


mouse.KeyDown:connect(function(key)
    if debounce == false and key == 'q' then
        debounce = true
        print('how about no')
        elseif debounce == true and key == 'q' then
        debounce = false
        print('kid friendly word')
    end
end)
0
I kind of wanted to use UserInputService since KeyDown is deprecated. Precisionly 103 — 6y

Answer this question