How do I limit animation spam

So I have made a custom hit animation with a tool but now people can spam the animation by pressing it multiple times

I tried using debounce but it doesn’t seem to work :confused:

This is the Local script I’m using right now.

local debounce = false



script.Parent.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Hit)
		if debounce == false then
            debounce = true
			animation:Play()
			wait(1)
			debounce = false
		else
			animation:Stop()
		end
		
	end)
end)

script.Parent.Unequipped:Connect(function()
	animation:Stop()
end)

EDIT: grammar.
EDIT 2&3: Wrong code.

Your current code creates a new connection every time they equip the tool. Try this.

local debounce = false
local equipped = false
local mouse = game.Players.LocalPlayer:GetMouse()
animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Hit)

script.Parent.Equipped:Connect(function()
   equipped = true
end)

mouse.Button1Up:Connect(function()
    if debounce == false and equipped == true then
       debounce = true
       animation:Play()
       wait(1)
       debounce = false
    end
end) 


script.Parent.Unequipped:Connect(function()
	animation:Stop()
    equipped = false
end)
2 Likes

Hmm I can still spam it :confused:
TNISct1cgM

If you press the button mid animation it resets I think I need to disable that somehow.

I edited the code a bit, try it now.

Still spammable
:confused: Problem is they can make the animation play before it even finished

Hmm, thats strange. I’ll test and fix the code on my end and get back to you when I have a solution.

1 Like

So I have been a little brainless :smiley: I edited the tool that was in workspace but your code seemed to work better, sorry for time waste and thanks for the help!

Oh no worries, glad it works now!