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)
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.
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
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.