Animation stays after unequip tool

Hello! I have the following piece of code:

local tool = script.Parent
local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/Asset?ID=10499840369"
local track

tool.Equipped:Connect(function()
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
	track.Priority = Enum.AnimationPriority.Idle
	track.Looped = true
	track:Play()
end)

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

which is responsible for loading an animation and then stopping it after the tool is no longer equipped. It is a local script under the tool itsself. How would I make it so the animation actually stops after unequipping the tool? Right now the animation just stays after its unequipped.

LoadAnimation is deprecated, use an animator. Humanoid:WaitForChild(“Animator”):LoadAnimation()

1 Like

You’re still loading a new animation track each time the tool is equipped.

if not track then --Only load the animation track once.
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
end
1 Like

Right, that works but how would I stop the animation once the tool has been equipped?

I believe :Stop() would work once using that.

Yeah that’s the problem, that doesn’t work.

As Forummer said, only load the animation if it doesn’t exist in the equipped thing.

1 Like

Ah, I got it working! Thank you.

Np have a good day charsdfsdfsdfsdf