Animation stuck if you click two times

I have a tool and a LocalScript inside that plays an animation once you click and if you un-equip it, it stops playing the animation. However, if you click it twice and un-equip, the animation gets stuck. How do I edit my script so that this doesn’t happen?

local hum 

local anim_feet 

local current 

local MouseConnection 

script.Parent.Equipped:connect(function(m)

MouseConnection = m.Button1Down:connect(function() 
hum = game.Players.LocalPlayer.Character.Humanoid

anim_feet = hum:LoadAnimation(script.Parent.Animation)

current = anim_feet

current:Play()

end)

end)

script.Parent.Unequipped:Connect(function()

anim_feet:Stop()

MouseConnection:Disconnect() 

end)

Hi!

Could this maybe have anything to do with “current” variable? What happens if you remove it, and load animation. It may also be useful to add wait(). Check the following script (also make sure you use Connect with the capital):

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

local MouseConnection
local anim_feet

script.Parent.Equipped:Connect(function(mouse)
	MouseConnection = mouse.Button1Down:Connect(function()
		anim_feet = hum:LoadAnimation(script.Parent.Animation) wait() -- waiting here
		anim_feet:Play()
	end)
end)

script.Parent.Unequipped:Connect(function()
	anim_feet:Stop()
	MouseConnection:Disconnect()
end)

Unfortunately, the edited script you sent me still plays a double animation once you click twice.

I forgot, you don’t have to load animation each time mouse is clicked. What if you move this statement

anim_feet = hum:LoadAnimation(script.Parent.Animation) wait() -- waiting here

outside MouseConnection function? I would still keep the wait() though.

1 Like