How to stop playing idle animation

im trying to stop the idle animation from playing but i keep getting this error message:
21:11:05.776 Players.TheBossOfBancon.Backpack.pitchfork.servertool:10: attempt to index nil with ‘Stop’ - Server - servertool:10

code block:

local tool = script.Parent
local idle = nil

tool.Equipped:Connect(function()
	local idle = tool.Parent.Humanoid.Animator:LoadAnimation(script:WaitForChild("idle"))
	idle:Play()
end)
	
tool.Unequipped:Connect(function()
	idle:Stop()
	idle:Destroy()
end)

local cooldown = 5
local lastActivation = 0

tool.Handle.Touched:Connect(function(part)
	if part.Name == "Handle" then
		local player = game.Players:GetPlayerFromCharacter(tool.Parent)
		local currentTime = tick()
		if currentTime - lastActivation >= cooldown then
			local use = tool.Parent:FindFirstChild("Humanoid"):LoadAnimation(script.use)
			use:Play()
			script.useS:Play()
			script.pick:Play()
			player.leaderstats.Cash.Value += 5
			game.ReplicatedStorage.remotes.cooldown:Fire()
			lastActivation = currentTime
		end
	end
end)

Instead of this:

Use this. It’s better because you don’t have to load the animation each time. It should also fix your error.

local animation = script:WaitForChild("idle")
local character = tool.Parent
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local idle = animator:LoadAnimation(animation)

tool.Equipped:Connect(function()
	idle:Play()
end)
	
tool.Unequipped:Connect(function()
	idle:Stop()
end)

i got this error:Infinite yield possible on ‘Players.TheBossOfBancon.Backpack:WaitForChild(“Humanoid”)’

Ah, well, that’s because, ‘tool.parent’ is the backpack, so it doesn’t work. Instead of this line:

use this line:

local character = tool.Parent.Parent.Character

This will only work if the tool is parented to the backpack when the player spawns.

1 Like

its is really confusing i got another error: 21:47:41.258 Cannot load the AnimationClipProvider Service. - Server - servertool:6

This is a very annoying error that has occurred several times before. It occurs when you try to load an animation before the character is loaded.

Here’s a fix for it:

local character = game.Workspace:WaitForChild(game.Players.LocalPlayer.Name)

Basically, this will wait for the character to load into the world.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.