Debounce for InputBegan and InputEnded

I’m trying to make this debounce work for both InputBegan and InputEnded. So when I press the button it prints “Pressed” and When I release it it prints “release” then has to wait a couple seconds to do it again. But it only works for the inputBegan function but not the inputEnded function

Here is the code for it

UIS.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.Q then
		if not debounce then
			debounce = true
			print("Pressed")
			--game.ReplicatedStorage.FireballEvent:FireServer(Mouse.Hit, "Charge")
		end
	end
end)

UIS.InputEnded:Connect(function(input,gpe)
	if input.KeyCode == Enum.KeyCode.Q  then
		if debounce then
			debounce = false
			print("release")
			--wait(2)
			--game.ReplicatedStorage.FireballEvent:FireServer(Mouse.Hit, "Fire")
		end
	end
end)
1 Like

Let me take a look as this should function fine.

I copied the script exactly and it worked fine.image_2021-11-12_133717

That part worked for me but now I want it to wait 2 seconds before I can press it again

Maybe do print(“release”) then add the wait(2) then do debounce = false in that order

Shouldn’t that work?

Yours currently looks like this

debounce = false
print("release")
--wait(2)

To make it work how I think you want it too, replace that code with this:

print("release")
wait(2)
debounce = false

You could also remove the ‘print(“release”)’ if you no longer need it for debugging

Yup, as I said. Thanks for making it into script form.:+1:t2:

1 Like

I commented it out cuz it didn’t work

just try replacing the code snippet I outlined with the one I provided and show what happens

Thats what happens when I do that

Thats because you are printing “release” whenevr you release the key, NOT when it goes off cooldown. Move the release to AFTER the wait and itll print whenever it goes off cooldown.

I want it to print pressed when I press the button, then when I release it, I want it to print released. Then after that I want the debounce to take effect when I try to press Q so it doesn’t print pressed when I press the button and released when I release it for 2 seconds.
Sorry for the poor explanation, I don’t know how to explain it properly.

add a print after the wait(2) and make it print something like “off cooldown” and see if it will print “pressed” before it prints “off cooldown”

It prints off cooldown first. Wouldn’t it make sense if I printed released then the wait since I want the debounce to work after the input response.

print("release")
wait(2)
debounce = false
print("off cooldown")

This is what I meant, I apologize for the misunderstanding. Try and see what happens and if you can press it before its prints “off cooldown”

Now it keeps printing released and off cooldown two seconds later

UIS.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.Q then
		if not debounce then
			print("Pressed")
			--game.ReplicatedStorage.FireballEvent:FireServer(Mouse.Hit, "Charge")
		end
	end
end)

UIS.InputEnded:Connect(function(input,gpe)
	if input.KeyCode == Enum.KeyCode.Q  then
		if not debounce then
			print("release")
			debounce = true
			wait(2)
            debounce = false
            print("off cooldown")
			--game.ReplicatedStorage.FireballEvent:FireServer(Mouse.Hit, "Fire")
		end
	end
end)

Maybe try this? Sorry for the unconfident answers, I cannot test them currently

Nah it didn’t work and it’s fine at least your helping me which I appreciate

Im home now, let me try and make something

local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.Q then
		if not debounce then
			print("Pressed")
			debounce = true
			--game.ReplicatedStorage.FireballEvent:FireServer(Mouse.Hit, "Charge")
		end
	end
end)

local onCooldown = false

UIS.InputEnded:Connect(function(input,gpe)
	if input.KeyCode == Enum.KeyCode.Q  then
		if debounce and not onCooldown then
			onCooldown = true
			wait(2)
			onCooldown = false
			debounce = false
			--game.ReplicatedStorage.FireballEvent:FireServer(Mouse.Hit, "Fire")
		end
	end
end)

This works how I imagine you wanted it to work, sorry for the false answers and the time it took me to respond, I wish you luck in making your game.