Walk animation won't stop playing?

So in my last post, I had a problem where my sword attack animations weren’t playing. I fixed that but now when I walk it won’t stop playing the animation. I’ve tried moving the code to different areas in the script and instead of using if statements i changed it to while loops but nothing is working! Any help?

spawn(function()
	while wait() do
		if script.Parent.Parent:FindFirstChildWhichIsA("Humanoid") then
			local walkanim = script.Parent.Parent.Humanoid:LoadAnimation(walk)
			local idleanim = script.Parent.Parent.Humanoid:LoadAnimation(idle)
			while script.Parent.Parent.Humanoid.MoveDirection.Magnitude <= 0 do
				wait()
				idleanim:Play()
				walkanim:Stop()
				repeat wait() until script.Parent.Parent.Humanoid.MoveDirection.Magnitude > 0
				if script.Parent.Parent.Humanoid.MoveDirection.Magnitude > 0 then break end
			end
			while script.Parent.Parent.Humanoid.MoveDirection.Magnitude > 0 do
				wait()
				idleanim:Stop()
				walkanim:Play()
				repeat wait() until script.Parent.Parent.Humanoid.MoveDirection.Magnitude <= 0
				if script.Parent.Parent.Humanoid.MoveDirection.Magnitude <= 0 then break end
			end
		end
	end
end)
1 Like

Maybe this script would be easier because the way you tried to run the walking animation is kind of weird. This is how I would make it:

local walkanim = script.Parent.Parent.Humanoid:LoadAnimation(walk)
local idleanim = script.Parent.Parent.Humanoid:LoadAnimation(idle)

humanoid.Running:Connect(function(speed)
    if speed < 1 then
		walkanim:Stop()
		idleanim:Play()
		print("Idle")
	end
	if speed > 1 and speed < 17 then
		walkanim:Play()
		idleanim:Stop()
		print("Walking")
	end
end)