Custom Idle animation doesn't play after walking/running

This is my first post on the dev forum, apologies if I’ve done anything incorrectly.

What I’m attempting to achieve
I have a tool/weapon that has an idle animation, and I’m trying to make it so that the idle animation plays whenever my character isn’t moving.

ISSUE
The idle animation plays when I equip the weapon, but after I walk or run, the idle animation doesn’t play until I re-equip the tool

Local script inside the tool is below:

local tool = script.Parent 
local anim = Instance.new("Animation") 
anim.AnimationId = "rbxassetid://9386775434" 
local track 

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local RunService = game:GetService("RunService")
	
tool.Equipped:Connect(function() 
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim) 
	track.Priority = Enum.AnimationPriority.Idle 
	track.Looped = true 
	track:Play() 
	RunService.RenderStepped:Connect(function()
		if humanoid.MoveDirection.Magnitude == 0 then 
			if not track then
				track:Play()
			end
		end
	end)
end)
tool.Unequipped:Connect(function() 
	if track then 
			track:Stop() 
	end
end)

RunService.RenderStepped:Connect(function()
	if humanoid.MoveDirection.Magnitude > 0 then
		if track then
			track:stop()
		end
	end
end)
RunService.RenderStepped:Connect(function()
	if humanoid.MoveDirection.Magnitude > 0 then
		if track then
			track:stop()
		end
	end
end)

This is where your issue lies. The track stops permanently the moment you move

What could I do to make it so the track can restart?

RunService.RenderStepped:Connect(function()
	if humanoid.MoveDirection.Magnitude > 0 then
		if track and tool.Unequipped == true then
			track:stop()
		end
	end
end)

Perhaps this script works

I just tried that, but now the idle animation plays when I walk as well as when I’m still.

RunService.RenderStepped:Connect(function()
	if humanoid.MoveDirection.Magnitude > 0 then
		if track and tool.Unequipped == true and not humanoid:GetState(Enum.HumanoidStateType.Idle) then
			track:stop()
elseif humanoid.MoveDirection.Magnitude > 0 and Tool.Equipped == true then
            track:Stop()
		end
	end
end)

Thanks for Continuing to help, but it it has the same result

I managed to fix it after a while, here’s the code:

local tool = script.Parent 
local anim = Instance.new("Animation") 
anim.AnimationId = "rbxassetid://9386775434" 
local track 

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local RunService = game:GetService("RunService")

tool.Equipped:Connect(function() 
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim) 
	track.Priority = Enum.AnimationPriority.Idle 
	track.Looped = true 
	track:Play()
end)  

tool.Unequipped:Connect(function() 
	if track then 
		track:Stop() 
	end
end)

RunService.RenderStepped:Connect(function()
	local equipped = false

	tool.Equipped:Connect(function()
		equipped = true
	end)

	tool.Unequipped:Connect(function()
		equipped = false
	end)

	if equipped == true then
		local humanoid = character:FindFirstChildOfClass("Humanoid") or character:FindFirstChildOfClass("AnimationController")
		local animator = humanoid:FindFirstChildOfClass("Animator")

		if humanoid.MoveDirection.Magnitude > 0 and track.IsPlaying then
			track:Stop()

		elseif humanoid.MoveDirection.Magnitude == 0 and tool.Equipped and not track.IsPlaying then
			for i,v in ipairs(animator:GetPlayingAnimationTracks()) do
				v:Stop()

			end

			track:Play()
		end
	end
end)