Prevent Punch Animation Spam

So, I’m trying to make a punch script and I’ve made the animations. I mainly have 1 problem. The first one being is animation spam. I can’t prevent the player from spamming the punch animation. I’ve tried debounce and looked on the forum and wiki for solutions, but it doesn’t help much.

A second issue but I can figure it out easily most likely. I just threw it in here

My second issue is I don’t want both keys to be pressed at the same time so as both the animations should play. It should be if I press Q, I can’t press E until the first animation is done. The second one I can probably figure out easily but I just mainly need help with my first problem.

Le Code

local debounce = false

UIS.InputBegan:Connect(function(key, code)
	if key.KeyCode == Enum.KeyCode.E then
		if not debounce then
			debounce = true
			local anim = player.Character:WaitForChild('Humanoid'):LoadAnimation(right)
			anim:Play()
			wait(2)
			debounce = false
			end
	end
end)

I’ve also tried using the debounce in different lines and even writing it differently, but I’m still able to spam the animation. What am I missing?

UIS.InputBegan:Connect(function(key, code)
	if key.KeyCode == Enum.KeyCode.E and not debounce then
			debounce = true
			local anim = player.Character:WaitForChild('Humanoid'):LoadAnimation(right)
			anim:Play()
			wait(2)
            anim:Stop()
			debounce = false
	end
end)

I’m not sure if this will work, but you can try it and see.

You forgot to declare what debounce is. At the beginning of your script just declare it like so:

local denounce = false

I think he has declared it but hasn’t shown that part of the script.

1 Like

Still spamming the animation for some reason. :confused:

I declared it my fault, I’ll include it for clarification.

Try to check the debounce this way.

UIS.InputBegan:Connect(function(key, code)
	if key.KeyCode == Enum.KeyCode.E then
		if debounce == false then
			debounce = true
			local anim = player.Character:WaitForChild('Humanoid'):LoadAnimation(right)
			anim:Play()
			wait(2)
			debounce = false
			end
	end
end)
UIS.InputBegan:Connect(function(key, code)
    if code then return end
    if not debounce then
        debounce = true
	    if key.KeyCode == Enum.KeyCode.E then
			local anim = player.Character:WaitForChild('Humanoid'):LoadAnimation(right)
			anim:Play()
			wait(2)
            anim:Stop()
         end
			debounce = false
       
	end
end)

Maybe something like this may work.

I have found an odd solution. I tried testing in a different place file and it worked. Then made a new one and it didn’t work at all. I did this back and forth with 5 different places and only 1 of the newly created ones did not work. 4/5 times it worked. So I don’t know if it’s a studio bug or anything but I’m glad I’m done with this. Felt like I was using debounce incorrectly.

Thanks for the help guys, much appreciated, sorry if I wasted your time since it wasn’t the code, twas a studio thing.

2 Likes