Combating Humanoid & Collision Lag

Heya! I’m working on an RPG that uses Roblox’s default humanoids to handle large amounts (100+) of NPCs at once. It’s pretty easy to see how this can cause lag. I spent some time early on making sure that the AI itself wouldn’t be eating up massive amounts of memory, and it actually doesn’t: however, I’ve run into another problem that causes humanoids to slow down.

My issue is that the large amount of collisions per frame that result from 30-50 parts per NPC with a large amount of NPCs overworks the physics engine, resulting in consistent, very noticeable lag. When testing in Studio, this lag equates to a framerate drop. When playing in an actual server, framerate itself is fine but the NPCs themselves are laggy due to the server handling physics instead of my PC. This lag equates to them moving slowly–far slower than they should be with their WalkSpeed.

I see two potential workarounds for this: I could try playing with Physics Ownership of nearby NPCs so the NPCs closest to the player run better, or I could experiment using custom humanoids with anchored parts (to ease the physics strain) that have a CFrame-based movement system. Each of these would take a lot of time, however, and I’m not the best at CFraming so custom humanoids is a last resort for me. Does anybody who’s encountered this issue before have feedback/advice on what works and what doesn’t, or ideas for an alternative fix?

4 Likes

You could probably use SetStateEnabled for each humanoid and turn off unnecessary states that just cause lag, like the Climbing or Swimming state. Don’t turn it off if you are actually using the state, though.

3 Likes

Already disabled almost all of the humanoid states, although good advice because it definitely does help.

Most of the RTS games that I know of don’t use Humanoids for this reason. Some Roblox employees I know made a RTS game using 2D sprites. Other games I know of use physics objects, billboard GUIs, and welds. I’ve made a game with animation controllers.

The difference between a RTS game and RPG game is that usually there isn’t a large number of NPCs being viewed at once. You can unload NPCs that are not near players. Many RPG games implement a fog of war and don’t allow third person cameras, restricting the number of assets that need to be loaded even more.

1 Like

Hey, I believe I covered this topic in this post here.

2 Likes

Thanks for this, appreciate it! Due to the nature of my game’s combat, I don’t think the whole anchoring solution you described is possible, but if you ever open-source your NPC service I’d love to take a look at it and see what you’re doing to make things work, because it sounds pretty interesting.

You can check out updates and changes here.

I wouldn’t really call it open source personally. But it looks like Roblox is adding package sharing and such in the future. I’d be more open to calling it open source then tbh as from what it sounds like people will be able to upload changes to the same package instead of making duplicates. IMO that goes more in line with open source than anything else.

I simply uncopylocked it for others to use. But if you do make changes that improved performance I wouldn’t mind if you sent them back to me so I could update the module with it. Do be aware, my 300NPC solution hasn’t been uploaded to the module yet. So the link you’ll see inside the post is the previous version.