How often you guys make npc moveTo target

I have a npc handler for NPC which checks to see if any players are nearby and attacks them and I loop every 0.2s for each npc with coroutine wrap. I don’t know if this gonna cause lag later on.

I would probably do it once every 0.5 to 1 second for searching and then a faster time for when they’re active. But if you’re having a very large number of NPCs, you might look into crowd solutions. In that case, you’d have a crowd of NPCs (that are near each other) do one calculation for the entire group.

There are a few ways of doing that. One method might be to just spawn them in groups and have them always work as a group. Another method would be to group them dynamically, which there are multiple ways of doing as well, however I would do that less frequently as that’s a bit more costly.

1 Like

Thanks for the response, how many npc do you think I should put in a group? I was testing with around 150 npcs and the server was starting lagging making skills becoming slow. grouping npc is a better solution.

The larger the group the better, however that may reduce accuracy.

There is an alternative method that doesn’t require grouping, where you only check the distance for each player instead of every NPC checking their own distance. You can make this even faster by only iterating through nearby NPCs by using something like GetPartsInRadius, however that may be a more expensive call.

Though grouping may still be useful when dealing with a large number of active NPCs.

I’m thinking of setting regions with touched or something similar to detect if a player enters that region and starts the loop.

I will test that region method together with grouping method tomorrow.

I would recommend doing the active check per player instead as that’s likely much cheaper, but that can work too.

That sounds good. Hopefully you’ll find a good solution :+1:
I’m sure there are some tutorials or explanations on YouTube that would cover this sort of logic for crowd grouping or even crowd AI.