Stopping a tool animation when tool destroyed

im having a problem with the equip animation still playing while the tool is already destroyed

local Tool = script.Parent
local humanoid = nil
local drinker = nil
local con = nil
local enabled = false
local db = false
local tool = script.Parent
local anim = Instance.new(“Animation”)
anim.AnimationId = “http://www.roblox.com/Asset?ID=17590050707
local track
tool.Equipped:Connect(function()
track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
track.Priority = Enum.AnimationPriority.Action
track.Looped = true
track:Play()
end)
tool.Unequipped:Connect(function()
if track then
track:Stop()
end
end)
function onEquipped(mouse)
humanoid = Tool.Parent:FindFirstChild(“Humanoid”)
if humanoid ~= nil then
con = mouse.Button1Down:connect(function() onButton1Down(mouse) end)
drinker = humanoid:LoadAnimation(Tool.anim)
end
end
function onUnequipped(mouse)
humanoid = nil
if drinker ~= nil then
drinker:remove()
drinker = nil
end
if con ~= nil then
con:disconnect()
end
end
function onButton1Down(mouse)
if enabled then
return
end
enabled = true
drinker:Play()
task.wait(1)
enabled = false
end
Tool.Equipped:connect(onEquipped)
Tool.Unequipped:connect(onUnequipped)

check if tool was destroyed then stop the animation and the connections.

2 Likes

1. remove() function (line 32)
The AnimationTrack class does not have a remove() method, you can check this if you look at its documentation - AnimationTrack | Documentation - Roblox Creator Hub

Perhaps you meant to write a drinker:Stop() instead?

2. Review your script, as it has two different variables pointing to the same tool.

1. local Tool = script.Parent
...
7. local tool = script.Parent

You also have 2 functions connected to the same Equipped and Unequipped events.

As a result, it is possible that the sequence of commands is mixed up somewhere, although this may not be the case.

1 Like