Animation not stopping after unequipping tool

So I am trying to make the animation play when I equipped the tool and making the animation stop playing when unequipped.

But the problem is when I unequip the animation keeps on playing(btw the animation is set to Action, and I’ve also tried all the others and I got the same result).

When equipped:

When unequipped:

This is the code that’s in the tool:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local WeaponTool = script.Parent


script.Parent.Equipped:Connect(function()

	game.ReplicatedStorage.ConnectM6D:FireServer(WeaponTool.BodyAttach)

	char.UpperTorso.ToolGrip.Part0 = char.UpperTorso
	char.UpperTorso.ToolGrip.Part1 = WeaponTool.BodyAttach
	char.Humanoid:LoadAnimation(script.Animation):Play()
end)

script.Parent.Unequipped:Connect(function()
	game.ReplicatedStorage.DisconnectM6D:FireServer()
	char.Humanoid:LoadAnimation(script.Animation):Stop()
end)

As you can see, I stated to stop the animation upon unequipping.

And this is the script that’s in the ServerScriptService

game.Players.PlayerAdded:Connect(function(plr)			
	plr.CharacterAdded:Connect(function(char)

		local M6D = Instance.new("Motor6D", char.UpperTorso)
		M6D.Name = "ToolGrip"
	end)
end)

game.ReplicatedStorage.ConnectM6D.OnServerEvent:Connect(function(plr,location)

	local char = plr.Character
	char.UpperTorso.ToolGrip.Part0 = char.UpperTorso
	char.UpperTorso.ToolGrip.Part1 = location


end)

game.ReplicatedStorage.DisconnectM6D.OnServerEvent:Connect(function(plr)
	plr.Character.UpperTorso.ToolGrip.Part1 = nil
end)

Also this is my view:
image

The output is saying nothing, please I need help on getting an understanding of what I am doing wrong.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local WeaponTool = script.Parent


script.Parent.Equipped:Connect(function()

	game.ReplicatedStorage.ConnectM6D:FireServer(WeaponTool.BodyAttach)

	char.UpperTorso.ToolGrip.Part0 = char.UpperTorso
	char.UpperTorso.ToolGrip.Part1 = WeaponTool.BodyAttach
	local Track = char.Humanoid:LoadAnimation(script.Animation)
    Track:Play()

 script.Parent.Unequipped:Connect(function()
	game.ReplicatedStorage.DisconnectM6D:FireServer()
    Track:Stop()
 end)

end)
1 Like

thanks man :slightly_smiling_face:. But why did you use track instead? I’m just curious and trying to get better at scripting. :sweat_smile:

Track is a variable, LoadAnimation returns a track, and you were loading the animation again instead of stopping it when unequipping, so thats why it didn’t work.

oh ok, thanks for the advice. I’ll use this to my advantage.