Problem with tool idle and walking animations

Hello, I have 2 animations. A tool walk and a tool idle. The script works perfectly fine however the animations just keep glitching.

Here’s a video:


Apologies if it’s laggy, I really don’t know why.

So you can see how glitched the animations looks and I believe this to be because of the animation priority. However, I’ve experimented with the priorities and they still look glitched. Here’s my script.

---{Variables}
local rs = game:GetService("ReplicatedStorage")
--
local tool = script.Parent
local model = tool:FindFirstChild("Particles")
--
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:FindFirstChild("Humanoid")

local walk = humanoid:LoadAnimation(script.Walk)
local idle = humanoid:LoadAnimation(script.Idle)

local equipped = false
---

tool.Equipped:Connect(function()
	equipped = true
	humanoid = tool.Parent:FindFirstChild("Humanoid")	
	idle.Priority = Enum.AnimationPriority.Action
	walk.Priority = Enum.AnimationPriority.Action2
	idle:Play()
	local idling = false
	local walking = false

	coroutine.wrap(function()
		while wait() do
			if equipped == false then return end
			if humanoid.MoveDirection.Magnitude ~= 0 then
				idle:Stop()
				idling = false

				coroutine.wrap(function()
					if walking == false then
						walking = true
						walk:Play()
					end
				end) ()

				if humanoid.MoveDirection.Magnitude == 0 then
					walking = false
					walk:Stop()
					idling = true
					idle:Play()
				end
			else
				walking = false
				walk:Stop()
				if idling == false then
					idling = true
					idle:Play()
				end
			end
		end
	end) ()
end)

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

And yes, I’ve tried changing the idle animation’s priority to idle and it’s even more glitched than action. Probably because I used easing styles for it.

I didn’t understand what you tried to say.You mean idle animation is glitching when walking? or the idle and walk animations are glitching when walking?