Game lagging with over 100 npc(script performance is only2-3 %)

I do not know why my game lagging. I use 1 script to control all Npc and it only takes 2-3% in script performance. I think it would be about physical contact between NPC, but I am not sure. Can someone help. I can’t show my code because it is confidential for my game. I just want to know what cause it lagging.

https://gyazo.com/d99312f64b1ee06c9cc40bc05b9d45ff

There are probably lots of events being fired at one time, variable being set, etc., with over 100 npcs firing them.

Try reducing the amount of NPCs you have.

2 Likes

It’s probably all the physics. Try adding a collision group for the zombies.

I am trying to make a game that has a lot of zombies. I tested with 64 zombies. It works fine, but I feel like it is too little for a survival zombie game.

yea, I just want to know if there are other factors besides it.

Humanoids take up a ton of resources on whoever is simulating them. While your scripts themselves might not be having too much execution time, I’d wager your heartbeat/physics step is slow.

Use the profiler in studio via CTRL F6 and CTRL P to see where slowdowns are.

How should I fix it? I never hear of it before

If you can, use less humanoids.
If you can’t, disable all unused humanoid states. This will reduce physics step execution time.

Can I spawn zombies whenever there are people around and despawn it when there are none?

What I do is check if there is any player around a certain NPC, if so the script continues to work, else it waits until a player is near by to continue. (I also use one script for all my NPC)