Tool animation wont stop after unequip

Hello, so im making this game with frying pan as the tool but whenever i equip the tool it plays an idle animation for the tool, everything works perfectly until you enequip the tool, it wont stop the animation, can someone help?

demonstration

heres the script thats responsible for he animation (server script because its needed for tool handle to be animated)

local tool = script.Parent
local player = nil
local char = nil
local pan = nil
															--s
tool.Equipped:Connect(function()
	player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
	char = player.Character

	pan = game.ServerStorage.Pans.PansModel.Default.BodyAttach:Clone()
	pan.Parent = char
	local motor = Instance.new("Motor6D")
	motor.Parent = pan
	motor.Name = "PanHandleControl"
	motor.Part0 = char["Right Arm"]
	motor.Part1 = pan
	motor.Enabled = true
	
	local anim = game:GetService("ServerStorage").Anims:FindFirstChild("PanEquip")
	local animtrack = char:FindFirstChild("Humanoid"):LoadAnimation(anim)
	local anim2 = game:GetService("ServerStorage").Anims:FindFirstChild("PanIdle")
	local animtrack2 = char:FindFirstChild("Humanoid"):LoadAnimation(anim2)
	animtrack:Play()
	animtrack2:Play()
end)

tool.Unequipped:Connect(function()
	pan:Destroy()
	if char["Right Arm"]:FindFirstChild("PanHandleControl") then
		char["Right Arm"].PanHandleControl:Destroy()
	end
	local anim = game:GetService("ServerStorage").Anims:FindFirstChild("PanEquip")
	local animtrack = char:FindFirstChild("Humanoid"):LoadAnimation(anim)
	local anim2 = game:GetService("ServerStorage").Anims:FindFirstChild("PanIdle")
	local animtrack2 = char:FindFirstChild("Humanoid"):LoadAnimation(anim2)
	animtrack:Stop()
	animtrack2:Stop()
end)

1 Like

You should load the animations into the variables anim and anim2 at the beginning of the script so it doesn’t have to search and load them each time you equip and unequip them.

I used this: playing stop on an animation won’t stop it in the Search tool up top and found a bunch of solved posts on this same topic.

1 Like

im still confused, because im not a script specialist, i put those variables at the top and it giving me “Players.Android_TSRP.Backpack.Default.Mainscript:6: attempt to index nil with WaitForChild” error. and yes i included the search thing you mentioned and applied animationController. heres the updated script.

local tool = script.Parent
local player = nil
local char = nil 
local pan = nil
local anim = game:GetService("ServerStorage").Anims:FindFirstChild("PanEquip")
local animtrack = char:WaitForChild("Humanoid"):LoadAnimation(anim)
local anim2 = game:GetService("ServerStorage").Anims:FindFirstChild("PanIdle")
local animtrack2 = char:WaitForChild("Humanoid"):LoadAnimation(anim2)
local animationController = Instance.new("AnimationController")
local animstop = animationController:LoadTrack(animtrack and animtrack2)
local anims = animationController:LoadAnimation(animtrack and animtrack2)
															--s
tool.Equipped:Connect(function()
	player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
	char = player.Character
	animationController.Parent = player.Character
	pan = game.ServerStorage.Pans.PansModel.Default.BodyAttach:Clone()
	pan.Parent = char
	local motor = Instance.new("Motor6D")
	motor.Parent = pan
	motor.Name = "PanHandleControl"
	motor.Part0 = char["Right Arm"]
	motor.Part1 = pan
	motor.Enabled = true
	
	
	anims:Play()
end)

tool.Unequipped:Connect(function()
	pan:Destroy()
	if char["Right Arm"]:FindFirstChild("PanHandleControl") then
		char["Right Arm"].PanHandleControl:Destroy()
	end
	animationController:Destroy()
	anims:Stop()
end)

You have set char as nil before the :WaitForChild() executes, you can’t use :WaitForChild() for Nil types; you are basically trying to search “Humanoid” in nil.

so how do i fix, i had my friend scripted it but he’s now offline for a couple of days

Belongs in #help-and-feedback:scripting-support.

nevermind the roblox built in ai fixed this issue, thanks for helping

1 Like

Please mark @proturk_yt’s answer as the Solution if that was what fixed it.

1 Like