Too much NPCs for a for loop to handle

My game currently has over 134 Npcs in it. So whenever a player uses a move, it practically crashes their device if it’s not good enough to handle a for loop that’s going through the entirety of the folder of npcs to damage.

Does anyone know how I can optimize this without causing dramatic delay between damaging npcs?

One thing you can consider to reduce SOME performance problems is disabling unnecessary humanoid states. To my knowledge, humanoids have been infamous for performance problems, and this MIGHT be something you can try. (Take this with a grain of salt, please.)

I am not over a hundred NPCS yet (around 25 so far) but i am aiming for 2k or more. I built from the ground up to tackle this problem.

The secret is to only check NPCS that are likely to be in combat. No one is in the ice lair? Drop them from the loop.

Also, I keep running tables of what is attacking what. You attacked? You were attacking this goblin, so this goblin takes damage. Direct links, not for loops.

Do these checks on the server, and it should not get bogged down.

1 Like

You can set their NetworkOwnership to the server, If the loop handles each NPC one by one, You can try using coroutine.wrap() or task.spawn() so it runs the code without needing to await for the other ones to finish.

You can also try changing the Workspace’s SignalBehavior to deffered. As doing this will hopefully reduce the amount of Events that gets fired every second.

There’s also more stuff you can change on the Workspace, Like PhysicsSteppingMethod, InterpolationThrottling and many more. All of that stuff can and will Optimize your game. But of course, You also need to ensure your code is also clean and adapted to them.

(P.S: You can also try using Quenty’s Maid Module to handle tasks and to reduce Memory-leaks)

1 Like