My tool animation won't stop

I’ve recently been trying to make a tool with animation, and when it touches a specific brick the animation should stop and the tool should be removed from the player’s inventory.

local tool = script.Parent
local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/Asset?ID=ID" --"ID" is a placeholder
local track
tool.Equipped:Connect(function()
    track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
    track.Priority = Enum.AnimationPriority.Action
    track.Looped = true
    track:Play()
    tool.Handle.EquipSound:Play()
end)

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

tool.Handle.Touched:Connect(function(part)
  if part.Name == "RobberyBound" then
	 if part:FindFirstChild("Main") then
	  	 if part.Main:IsA("Script") then
			track:Stop()
		 end
	 end
  end
end)

The script provided above is the tool’s animation script. And that script executes very well. Also, before you read the script for the detector brick. Keep in mind that the detector brick will be mentioned under the name "RobberyBound".

script.Parent.Touched:Connect(function(part)
local h = part.Parent:findFirstChild("Humanoid")
local anim = script.Parent.Animation
anim.AnimationId = "http://www.roblox.com/Asset?ID=ID"
local track
if (h ~= nil) then
	local thisplr = game.Players:findFirstChild(h.Parent.Name)
	if (thisplr~=nil) then
		local stats = thisplr:findFirstChild("leaderstats")
		if (stats~=nil) then
			local score = stats:findFirstChild("Money")
			if (score~=nil) then
				if thisplr.Character:FindFirstChild("StoreBag") then
					thisplr.PlayerGui.StoreMoneyEarnUI.Frame.Visible = true
					thisplr.Character.StoreBag.Enabled = false
					thisplr.Character.StoreBag:Destroy()
					track = h:LoadAnimation(anim)
					track.Priority = Enum.AnimationPriority.Idle
					track.Looped = true
					track:Play()
					script.Parent.UnequipSound:Play()
					script.Parent.SFX:Play()
					score.Value = score.Value + 1250
					track.Looped = false
					wait(3)
					thisplr.PlayerGui.StoreMoneyEarnUI.Frame.Visible = false
					for i = 1,50 do
						thisplr.PlayerGui.music.Volume = thisplr.PlayerGui.music.Volume - 0.01
						wait()
					end
					thisplr.PlayerGui.music:Destroy()
				elseif thisplr.Backpack:FindFirstChild("StoreBag") then
					thisplr.PlayerGui.StoreMoneyEarnUI.Frame.Visible = true
					thisplr.Backpack.StoreBag.Enabled = false
					thisplr.Backpack.StoreBag:Destroy()
					track = h:LoadAnimation(anim)
					track.Priority = Enum.AnimationPriority.Idle
					track.Looped = true
					track:Play()
					script.Parent.UnequipSound:Play()
					script.Parent.SFX:Play()
					score.Value = score.Value + 1250
					track.Looped = false
					wait(3)
					thisplr.PlayerGui.StoreMoneyEarnUI.Frame.Visible = false
					for i = 1,50 do
						thisplr.PlayerGui.music.Volume = thisplr.PlayerGui.music.Volume - 0.01
						    wait()
					    end
					    thisplr.PlayerGui.music:Destroy()
				    end
			    end
		    end
	    end
    end
end)

The code above is inside the detector brick and it is supposed to be a script that executes a function if a player with the tool touches it. The problem is that when it executes, the tool animation continues to play even though the tool has already been removed from the player’s backpack.

I’ve tried a few things to stop this animation from running afterward. The code below was implemented in the first script for the purpose of stopping the animation when the tool touches the brick.

tool.Handle.Touched:Connect(function(part)
  if part.Name == "RobberyBound" then
	 if part:FindFirstChild("Main") then
	  	 if part.Main:IsA("Script") then
			track:Stop()
		 end
	 end
  end
end)

On the second script, it even tries to play the default running animation. And both tactics failed, the animation stopped twice but it was no use afterward.

Any help or suggestions is greatly appreciated :slightly_smiling_face:.

.
I am pretty sure you don’t need this. Unless you are looping it ofc. In which case you don’t need this.

track.Looped = false

Though it doesn’t need to be looped, it still doesn’t fix the issue

What if you load the animation before equipping the tool?

local tool = script.Parent
local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/Asset?ID=ID" --"ID" is a placeholder
local track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
tool.Equipped:Connect(function()
--Track play, etc.
end)

Unfortunately, it doesn’t seem to fix anything. I think the issue might be coming from the 2nd script.

Wait a minute.

How are you referencing track

Sorry for not making it known, but I made some changes to the code after I made the post.

you need to turn requireshandle off if the tool has a handle I suggest you weld it to the hand with a script

I have done so, but the main issue still persists.