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

Stopping a function after a key is finished pressing?

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

0

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

2 answers

✓ Accepted answer

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

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)

1 comment

0

I kind of wanted to use UserInputService since KeyDown is deprecated.

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