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)
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)