Animation track wont stop when tool is unequipped

I have a tool where once you equip it, an animation plays. But for some odd reason, when you unequip it, it doesn’t stop the animation track. Help please, here’s the script.

local tool = script.Parent

local sound = tool.Handle.Sound

local idleanim = Instance.new(“Animation”)
idleanim.AnimationId = “rbxassetid://5743938093”
local track1

local shootanim = Instance.new(“Animation”)
shootanim.AnimationId = “rbxassetid://5736412338”
local track2

tool.Equipped:Connect(function()

track1 = script.Parent.Parent.Humanoid:LoadAnimation(idleanim)
track1.Priority = Enum.AnimationPriority.Movement
track1.Looped = true
track1:Play()

tool.Activated:Connect(function()

  track2 = script.Parent.Parent.Humanoid:LoadAnimation(shootanim)
  track2.Priority = Enum.AnimationPriority.Movement
  track2.Looped = false
  track2:Play()
  wait(.5)
  sound:Play()
  tool.Unequipped:Connect(function()
  	
  	track2:Stop()
  	track1:Stop()

end)

end)

end)

Did you stop the “sound”? Try putting sound:Stop ()

works in studio, doesnt work in actual game

Try putting each function separate from each other, for what i can see in the script all of the functions are inside of tool.Equipped:Connect(function().

local tool = script.Parent

local sound = tool.Handle.Sound

local idleanim = Instance.new("Animation")
idleanim.AnimationId = "rbxassetid://5743938093"
local track1

local shootanim = Instance.new("Animation")
shootanim.AnimationId = "rbxassetid://5736412338"
local track2

tool.Equipped:Connect(function()
	
	track1 = script.Parent.Parent.Humanoid:LoadAnimation(idleanim)
	track1.Priority = Enum.AnimationPriority.Movement
	track1.Looped = true
	track1:Play()
	
end)

tool.Activated:Connect(function()
	
	track2 = script.Parent.Parent.Humanoid:LoadAnimation(shootanim)
	track2.Priority = Enum.AnimationPriority.Movement
	track2.Looped = false
	track2:Play()
	wait(.5)
	sound:Play()
	
	
end)

tool.Unequipped:Connect(function()
	
	track2:Stop()
	track1:Stop()
	
end)

Try using this script instead

Screenshot_94

I think the problem is that your tool unequipped event is inside of your tool activated event. Try Moving it out of the tool activated event.

If this works please give it a solution.

That probably happened cause you need to activate the tool in order to assing the track2 variable to the animation. You can use an if statement to make sure trak2 is not nil

Put track1 = script.Parent.Parent.Humanoid:LoadAnimation(idleanim) outside of Equipped and Put track2 = track2 = script.Parent.Parent.Humanoid:LoadAnimation(shootanim) outside Activated and it should work.

This is caused when you track1 and track2 in the function instead of it being outside of it.
local tool = script.Parent

local sound = tool.Handle.Sound

local idleanim = Instance.new(“Animation”)
idleanim.AnimationId = “rbxassetid://5743938093”
local track1 = script.Parent.Parent.Humanoid:LoadAnimation(idleanim)

local shootanim = Instance.new(“Animation”)
shootanim.AnimationId = “rbxassetid://5736412338”
local track2 = script.Parent.Parent.Humanoid:LoadAnimation(shootanim)

tool.Equipped:Connect(function()

track1.Priority = Enum.AnimationPriority.Movement
track1.Looped = true
track1:Play()

tool.Activated:Connect(function()

  track2.Priority = Enum.AnimationPriority.Movement
  track2.Looped = false
  track2:Play()
  wait(.5)
  sound:Play()
  tool.Unequipped:Connect(function()
  	
  	track2:Stop()
  	track1:Stop()

end)

end)

end)