NPC Walking Animation Not Playing Correctly During Movement

I wanted an NPC that has the walking animation playing (correctly) when it moves. However, after the first point the NPC moves to, its animation started to look rather…strange. I’ve tried longer waiting times between each move but it still happens, and I’m not sure if this is a loading issue. How do I fix this?

Video:

Here’s the code I used

function npcWalk(NPC, target)
	local humanoid = NPC.Humanoid
	
	local walkAnim = NPC.Animate.walk.WalkAnim
	local walkTrack = humanoid:LoadAnimation(walkAnim)
	
	local idleAnim = NPC.Animate.idle.Animation1
	local idleTrack = humanoid:LoadAnimation(idleAnim)
	
	idleTrack:Stop()
	humanoid:MoveTo(target.Position)
	walkTrack:Play()
	
	humanoid.MoveToFinished:Connect(function(reached)
		if reached then
			walkTrack:Stop()
			idleTrack:Play()
		end
	end)
end

wait(3)
npcWalk(workspace.RobertoNPC, workspace.testPart)
wait(3)
npcWalk(workspace.RobertoNPC, workspace.testPart2)
1 Like

Could you describe what exactly is wrong with the animation? I don’t see any major difference

2 Likes

you’re loading the animations on top of eachother, so its playing the idle animation and walking animation at the same time while they’re on the same layer, resulting in both animations being “mixed”.

The reason this happens is because well, you’re loading different animations each time the function is called. The original idle animation never stops playing, each time you called the function a new idle and walk animation would be created under a new variable, so calling the animation to be stopped would do nothing as each time the function is called, its a new animation.

The fix to your code is to simply move the 2 animation loads outside the function, and you should load animations onto an animator, not a humanoid.

local humanoid = NPC.Humanoid

local walkAnim = NPC.Animate.walk.WalkAnim
local walkTrack = humanoid.Animator:LoadAnimation(walkAnim)

local idleAnim = NPC.Animate.idle.Animation1
local idleTrack = humanoid.Animator:LoadAnimation(idleAnim)

function npcWalk(NPC, target)
	idleTrack:Stop()
	humanoid:MoveTo(target.Position)
	walkTrack:Play()
	
	humanoid.MoveToFinished:Connect(function(reached)
		if reached then
			walkTrack:Stop()
			idleTrack:Play()
		end
	end)
end

I’d also put the idle animation on the “core” layer and the movement animation on the “movement” layer to further avoid issues.

You could also just copy the client animate script, paste the code into a server script in the NPC and then remove any reference to “player” in the code, and that’ll get you a working script.

3 Likes

When the NPC moves to the second checkpoint, its limbs aren’t extending fully.