Animation not stopping after unequipped

Hello, i made this script and im trying to make it where if there is an animation playing when the tool is unequipped it stops but this kinda works and sometimes it still plays

 wait(1)
local OrginialShirt = game.Players.LocalPlayer.Character.Shirt.ShirtTemplate
local OrginialPants = game.Players.LocalPlayer.Character.Pants.PantsTemplate

local animation = script.Parent.Model.Animation:WaitForChild('Animation')
local humanoid = script.Parent.Model:WaitForChild('Humanoid')
local Move = humanoid:LoadAnimation(animation)

local AnimationPlaying = false
local Lift = false

local LiftUp = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Model.Animation.LiftUpWithHand)
local Player = game.Players.LocalPlayer.Character

script.Parent.Equipped:Connect(function()
	local Player = script.Parent.Parent
	Player.Shirt.ShirtTemplate = "rbxassetid://900707891"
	Player.Pants.PantsTemplate = "http://www.roblox.com/asset/?id=5971135677"
	wait(1)
	LiftUp:Play()
	Lift = true
	wait(0.7)
	AnimationPlaying = true
	Move:Play()
end)

script.Parent.Unequipped:Connect(function()
	Player.Shirt.ShirtTemplate = OrginialShirt
	Player.Pants.PantsTemplate = OrginialPants
	if Lift then
	    Lift = false
		AnimationPlaying = false
		print("AnimationStopped")
		LiftUp:Stop()
		Move:Stop()
		if AnimationPlaying then
			print("AnimationStopped1")
			AnimationPlaying = false
			Move:Stop()
		end
	end
end)

The script works for me, here’s what I did.

wait(1)
local OrginialShirt = game.Players.LocalPlayer.Character.Shirt.ShirtTemplate
local OrginialPants = game.Players.LocalPlayer.Character.Pants.PantsTemplate

local animation = script.Parent:WaitForChild('Equip')
local humanoid = game.Players.LocalPlayer.Character:WaitForChild('Humanoid')
local Move = humanoid:LoadAnimation(animation)

local AnimationPlaying = false
local Lift = false

local LiftUp = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Holster)
local Player = game.Players.LocalPlayer.Character

script.Parent.Equipped:Connect(function()
	print('equipped')
	local Player = script.Parent.Parent
	Player.Shirt.ShirtTemplate = "rbxassetid://900707891"
	Player.Pants.PantsTemplate = "http://www.roblox.com/asset/?id=5971135677"
	wait(1)
	LiftUp:Play()
	print('animation liftup playing')
	Lift = true
	wait(0.7)
	AnimationPlaying = true
	Move:Play()
	print('animation move playing')
end)

script.Parent.Unequipped:Connect(function()
	Player.Shirt.ShirtTemplate = OrginialShirt
	Player.Pants.PantsTemplate = OrginialPants
	if Lift then
		Lift = false
		AnimationPlaying = false
		print("AnimationStopped")
		LiftUp:Stop()
		Move:Stop()
		if AnimationPlaying then
			print("AnimationStopped1")
			AnimationPlaying = false
			Move:Stop()
		end
	end
end)

Also, using humanoid to load animations is deprecated. Consider using Animator:

1 Like
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Human = Character:WaitForChild("Humanoid")
local Shirt = Character:WaitForChild("Shirt")
local Pants = Character:WaitForChild("Pants")
local OrginialShirt = Shirt.ShirtTemplate
local OrginialPants = Pants.PantsTemplate

local Model = script.Parent:WaitForChild("Model")
local Anim = Model:WaitForChild("Animation")
local Animation = Anim:WaitForChild("Animation") --is this correct? an instance named animation inside another instance named animation
local Humanoid = Model:WaitForChild('Humanoid')
local Move = Humanoid:LoadAnimation(Animation)
local LUWH = Anim:WaitForChild("LiftUpWithHand")
local LiftUp = Human:LoadAnimation(LUWH)
local Tool = script.Parent

local AnimationPlaying = false
local Lift = false

Tool.Equipped:Connect(function()
	local Player = Tool.Parent
	Player.Shirt.ShirtTemplate = "rbxassetid://900707891"
	Player.Pants.PantsTemplate = "rbxassetid://5971135677"
	task.wait(1)
	Lift = true
	LiftUp:Play()
	task.wait(1)
	AnimationPlaying = true
	Move:Play()
end)

Tool.Unequipped:Connect(function()
	local Player = Tool.Parent.Parent
	Player.Shirt.ShirtTemplate = OrginialShirt
	Player.Pants.PantsTemplate = OrginialPants
	if Lift or AnimationPlaying then
		Lift = false
		AnimationPlaying = false
		LiftUp:Stop()
		Move:Stop()
	end
end)

Is “Model” different from the character? I’m assuming it’s a model inside the tool. I made some slight changes however and made more references for ease.

1 Like