Implementing debounce

I am reworking my combat system but the animations can be replayed if you spam Q. I don’t know what is causing this and I have tried to implement a debounce however, I am unsuccessful.

UIS.InputBegan:Connect(function(input, Processed)
	if Processed then return end
	if input.KeyCode == Enum.KeyCode.Q and ran == 1 and Debounce == false then
		Animations['LeftPunch']:Play()
		wait(0.5)
		ran = 2
		Debounce = true
	elseif ran == 2 and Debounce == true then
		Animations['RightPunch']:Play()
		wait(0.5)
		ran = 1
		Debounce = false
	end
end)
1 Like

Try putting the denounce just inside in the function (one tab in).

1 Like

If you mean this, then it didn’t work.

	if Processed then return end
	if input.KeyCode == Enum.KeyCode.Q and ran == 1 and Debounce == false then
		Animations['LeftPunch']:Play()
		wait(0.5)
		ran = 2
		Debounce = true
	elseif ran == 2 then
		Animations['RightPunch']:Play()
		wait(0.5)
		ran = 1
		Debounce = false
	end
end)
1 Like

The reason that you can spam the animation is because of what order you set your debounces in. In order to make sure that you are not able to spam the animation, you need to set the debounces first, so that if they press Q again, it will check for the debounce first instead of playing the animation then checking the debounces.

New Code:

UIS.InputBegan:Connect(function(input, Processed)
	if Processed then return end
	if input.KeyCode == Enum.KeyCode.Q and ran == 1 and Debounce == false then
		ran = 2
		Debounce = true
		wait(0.5)
	    Animations['LeftPunch']:Play()
	elseif ran == 2 and Debounce == true then
		ran = 1
		Debounce = false	
		wait(0.5)	
        Animations['RightPunch']:Play()
	end
end)
2 Likes

Try this code sample:

local Debounce = false

UIS.InputBegan:Connect(function(input, Processed)

    if not Debounce then

        Debounce == true

        (your code)

        wait(How many sec you want)
        Debounce = false

    end

end)

For more info check out this tutorial:
Debounce

1 Like

Thank you! I adjusted some of it so that there is no delay for the animation.

1 Like