Animation Restart when you spam click

So I’m making a item where when you click the animation plays for the item, But when you click multiple times the animation restarts from the beginning of the animation.

for example when I click once the animation plays, but when you spam your mouse button the animation keeps restarting to the start of the animation.

Any help would be great!

script.Parent.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
		animation:Play()
	end)
end)

script.Parent.Unequipped:Connect(function()
	if animation ~= nil then
		animation:Stop()
	end
end)

1 Like

Forgot to mention, I’m trying to make it so that when you click one time the animation only plays once and when you click multiple times it will keep playing that animation instead of restarting.

1 Like

I would create a cool down that waits until your animation is finished playing. You can use this article to do so:
https://developer.roblox.com/en-us/api-reference/event/AnimationTrack/Stopped
Hope this helps, if it doesn’t just let me know!

1 Like

Yeah, I’m not really sure how to put that into my code though lol

1 Like

You can add a debounce

local D = true

script.Parent.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
if D == true then
         D = false
		animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
		animation:Play()
        animation.Stopped:Wait()
            D = true
             end
	end)
end)

script.Parent.Unequipped:Connect(function()
	if animation ~= nil then
		animation:Stop()
         D = true
	end
end)

@NavenTheNoob

1 Like

Ohh lol, i tried to put a wait instead of debounce

Ok did it work??.

The article shows you that you can do the following:

YourAnimation.Stopped:wait()

I’m not exactly sure what you want to happen, but if you want to not allow the player to click unless the animation has finished I would definitely do what @luisgamercooI231fan suggested.

1 Like

yeah it did, but whenever the animation finishes you can click again and it starts playing again (By the way my item is one use)

Do this instead


local D = true

script.Parent.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
if D == true then
         D = false
		animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
		animation:Play()

             end
	end)
end)

script.Parent.Unequipped:Connect(function()
	if animation ~= nil then
		animation:Stop()
        
	end
end)

Alright, it works. Thank you all so much for the help!!

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local tool = script.Parent
local animation = tool:WaitForChild("Animation")
local track = humanoid:LoadAnimation(animation)

tool.Unequipped:Connect(function()
	if animation.IsPlaying then
		animation:Stop()
	end
end)

tool.Activated:Connect(function()
	if animation.IsPlaying then
		return
	end
	
	animation:Play()
end)

I know you’re just fixing bugs but it’s best to provide better solutions.

Regarding the thread’s poster, don’t forget that the “.Activated” event of tool instances exists (it is fired on a tool whenever the player presses the left mouse button while that tool is equipped).

There is no need for a debounce here and using state variables/flags should be avoided when possible.