Problem with tool idle and walking animations (Reupload)

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

The idle animation works, but the stance of the character is really weird.
The walking animation is just completely glitched

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 the action priority

try changing the priorities to movement