Optimal way to handle Npcs?

I’m trying to create a map wherein npcs are scattered across various zones and will begin chasing players if within range.

My initial idea was to simply place a script within each npc that has a RunService to detect whenever a player is within range and etc. However, I’ve found that this method generally tends to generate lag when having npcs on a larger scale and often creates stuttering on the part of the npc.

Any ideas or suggestions for what is the most optimal way to handle all these npcs?

The lag is probably because you’re connecting to runservice for every single npc, where if you only have one script (and one connection) you won’t have nearly as much lag.

If you want the actual most optimal way to handle NPCs, you’d have to make them all client-sided, with the client mostly handling hitboxes, movement, models, and logic. This does make the game more vunerable to exploiters, but in a PvE scenario exploiting doesn’t have much of an effect on other players.

Interesting, how would the structure a script that handles all npcs look? I normally use RunService to constantly update the player position so that npcs accurately move towards them, but how would a single script controlling all npcs keep that updated for each npc?

If you just want to activate them, you could just disable their AI until the main script detects a hostile player nearby. However, it is much more effecient to simply have the main script also preform everything the old individual scripts did. You’d store all the NPCs into a table, represented as a dictionary with all the values needed to determine what do (e.g. their weapon, their target). For every thinking interval, you iterate through all the NPCs in the table instead of just one.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.