How to determine when an npc is walking or not?

I am trying to determine when an npc is walking because I am wanting to play a walking animation when the npc is walking. I’d also like to play an idle animation when it is not.

The issue is that the animation is either not playing when it should, or is playing, but doesn’t stop. It seems the humanoid state is not changing when it should be :man_shrugging:
1st example,
2nd example

I have check on the developer hub, I just can’t seem to find anything about this, which is why I am here. I’m attempting to use the Humanoid State , but it seems to be problematic.

This is how I have it setup.

    self.humanoid.StateChanged:Connect(function(oldState, newState)
		warn(newState)
		if newState == Enum.HumanoidStateType.Running then
			print(npcName, "running")
			self.animation_Idle:Stop()
			self.animation_Walk:Play()
		elseif newState == Enum.HumanoidStateType.RunningNoPhysics then
			print(npcName, "idling")
			self.animation_Walk:Stop()
			self.animation_Idle:Play()
		else
			
		end
	end)

Oh, I just use

humanoid:MoveTo()

to move the humanoid.

Am I just doing something wrong with the humanoid state? Or should I be going about this a different way?

1 Like

just edit the Animation script that your roblox character has then transfer it to a server script then modify it to make it work, then you just slap that server script inside the model of NPC done. It’ll handle everything for you, idle/walking/jumping etc.

1 Like

Thank you for your response!
I actually thought about that myself, but worried about it because too much for the server to handle. I plan to have like 600 npcs at once.

pretty sure any game would lag with that amount of npcs?, solution is just unrender the far npcs

1 Like

I can confirm that using the marked solution is not the optimal solution if you have 600+ npcs.

If you plan to have over 100 npcs, then you will need to accomplish your movement and animation goals in a single server script that iterates through all of the NPCs several times. You should also remove all forms of collision checking that your gameplay can allow (CanCollide, CanTouch, CanQuery). If you write a for loop that does only a single, simple operation without using if statements, the loop will run extremely fast, even for 1000 npcs. Do enough simple operations in a row, and the script will be able to decide where humanoids should move and start the run animations much faster than 60 times per second (i.e. no lag).

For stopping the movement animation, you should put a simple script that’s childed to the NPC’s Humanoid with something like the following:

humanoid.MoveToFinished:Connect(function(reached)
	runAnimationTrack:Stop()  --both track variables need to be declared above this function
	idleAnimationTrack:Play()
end)

Using the techniques I explained above, I was able to get 1,000 npcs moving around and doing some extra AI logic on top of the movement code, and the server scripts were still processing at 60 frames per second (i.e. no lag). The most important thing to boost performance that much is to remove as much collision checking as you can, as the NPCs were constantly colliding with things in my game, which caused severe lag. I suppose if I’d made my game bigger, with fewer things to run into, the collisions wouldn’t have been such a big deal.

1 Like