How to make idle animation work properly?

I currently want to make an idle animation, when the player isn’t moving and has the tool equipped the idle animation would play. The problem is that when a player is moving the idle animation still plays.

local tool = script.Parent.Parent
local IdleAnim = script.Parent.Idle
local loadAnim

tool.Equipped:Connect(function()
	local Char = tool.Parent	
	
	local animator = Char:WaitForChild("Humanoid"):WaitForChild("Animator")
	loadAnim = animator:LoadAnimation(IdleAnim)
	
	loadAnim:Play()	
end)

tool.Unequipped:Connect(function()
	loadAnim:Stop()
end)
3 Likes

try

local IdleAnim = script.Parent:WaitForChild("Idle")
1 Like

That isn’t the problem, the animation is working fine. I’m just trying to see if there is a way for the idle animation to stop when the player moves.

1 Like

Try this:

NOTE: not tested, probably won’t work, just use this script as a reference.

local tool = script.Parent.Parent
local IdleAnim = script.Parent.Idle
local loadAnim

tool.Equipped:Connect(function()
	local Char = tool.Parent	
	Char.Humanoid.Running:Connect(function(speed)
		if speed <= 0 then
			local animator = Char:WaitForChild("Humanoid"):WaitForChild("Animator")
			loadAnim = animator:LoadAnimation(IdleAnim)
		
			loadAnim:Play()
		else
			loadAnim:Stop()
		end	
	end)	
end)

tool.Unequipped:Connect(function()
	loadAnim:Stop()
end)

1 Like

you can mess with the values in the players animate script that they get when their character spawns

it should look something like this: (do idle instead of run)
image

2 Likes

This kinda works, but is quite buggy. I’m going to see what I can do to make it work better.

2 Likes

The major problem with the script was that even if the tool isn’t equipped but the player is idle the idle animation would still play. This updated version fixed it, there is still a slight delay but I think that is roblox’s running function’s fault.

local tool = script.Parent.Parent
local IdleAnim = script.Parent.Idle
local loadAnim

local Equipped = false

tool.Equipped:Connect(function()
	local Char = tool.Parent	
	
	Equipped = true
	
	local animator = Char:WaitForChild("Humanoid"):WaitForChild("Animator")
	loadAnim = animator:LoadAnimation(IdleAnim)
	
	loadAnim:Play()
	
	Char.Humanoid.Running:Connect(function(speed)
		if Equipped == false then return end
		if speed <= 0 then
			loadAnim:Play()
		else
			loadAnim:Stop()
		end	
	end)	
end)

tool.Unequipped:Connect(function()
	Equipped = false
	loadAnim:Stop()
end)
3 Likes

I’m glad that you figured out a fix to my basic update of your script, good luck on your game!

2 Likes