So I want my animation to stop when the tool is unequiped but it doesn’t. The script is a server script inside of the tools Handle.
Here is my code:
script.Parent.Parent.Unequipped:Connect(function()
local Character = script.Parent.Parent.Parent.Parent.Character
local Humanoid = Character.Humanoid
local AnimationTrack = Humanoid:LoadAnimation(Animation)
AnimationTrack:Stop()
end)
You are loading a new animation to the humanoid, and stopping the new animation, not the animation that was loaded when the tool was equipped. Could you show your full script?
Okay, I didnt know your script was that long. I am only going to make changes to the Equipped and Unequipped functions.
You want to make an empty variable at the start of the script. Then when someone equips the tool, you want to make the variable equal to the the animation that you want to play when tool is equipped. And then when the tool is unequipped, you can easily just mention the variable name, and then stop the animation by using :Stop function.
Replace this full code:
--Animations
local Animation = script.Animation
local Animation2 = script.Animation2
script.Parent.Parent.Equipped:Connect(function()
local Character = script.Parent.Parent.Parent
local Humanoid = Character.Humanoid
local AnimationTrack = Humanoid:LoadAnimation(Animation)
AnimationTrack:Play()
end)
script.Parent.Parent.Unequipped:Connect(function()
local Character = script.Parent.Parent.Parent.Parent.Character
local Humanoid = Character.Humanoid
local AnimationTrack = Humanoid:LoadAnimation(Animation)
AnimationTrack:Stop()
end)
With this updated code:
--Animations
local Animation = script.Animation
local Animation2 = script.Animation2
local AnimationTrack -- leaving this blank
script.Parent.Parent.Equipped:Connect(function()
local Character = script.Parent.Parent.Parent
local Humanoid = Character:WaitForChild("Humanoid")
AnimationTrack = Humanoid:LoadAnimation(Animation) -- makes the variable non-empty
AnimationTrack:Play()
end)
script.Parent.Parent.Unequipped:Connect(function()
local Character = script.Parent.Parent.Parent.Parent.Character
local Humanoid = Character:WaitForChild("Humanoid")
AnimationTrack:Stop() -- just putting 'Stop()' because variable is not blank
end)