What is the best way to spawn multiple enemies without lagging the game?

Hello, and I hope you are all having a great day!

As I am attempting to make this game come true, I was wondering if you could give the best advice when it comes to having different areas separated around the game, with a lot of enemies spawned in each.

I was fearful if that was going to heavily lag the game and cause the gameplay experience to be unplayable.

Example: If it was possible to summon 40 enemies onto an area, then another 40 on a different area that is far away.

Long story short, How can I make sure that the local client can’t lag when there is a lot of enemies or NPCs in the game?

2 Likes

The simple solution would be to only load enemies on the client when they’re in the same zone or nearby. Even without doing that, you should only have animations running when visible by the player to begin with. The real lag comes from moving parts and physics.

I would recommend using OOP to create an abstract enemy object for each npc therefore you can just disable the NPC animations and other stuff but then you can also bring it back.

I currently gave up on the use of Animation scripts simply because it was too buggy. Instead, I have animations played on their own by humanoid events, such as running with a speed higher than 0 or lower than 0 inside a customized script.

However, the enemies do not move until they spot a player or a “good” npc like an ally. So they can’t walk and cause lag on their own. Still, I was concerned if it has to do with the part counter as well.

So, like if I would’ve put enemies inside ReplicatedStorage locally until the player has arrived at a certain area where the enemies can be moved back onto Workspace locally? Is that what you are describing?

It doesn’t even need to be there, but yes that can work. A more complicated option would be to not have any enemies server-side and handle them mostly client-side with a few server-side checks to avoid exploitation. But I assume the former method would be a lot easier.

I was thinking of that. But yes, it is going to be very difficult considering not only abilities are mostly servers, but hackers or exploiters can still, even with server checks, hack the powers and the health of enemies, which is a double disaster.

That’s exactly why you handle things like that server-side. Think of this: the client only needs to handle rendering. Just spawn the enemies, keep them where they need to be (unless you trust the client with enemy movement) and play animations when needed. The server will handle all of the events, such as when damage is taken, when animations play, and where the enemy’s position should generally be. I would recommend a mix to keep the client feeling smooth while also keeping things secure and not stress the server.

2 Likes

I like the idea of playing animations locally, however I have questions:

  1. How would I go about enemies attacking at a certain animation event for some of the animations?
  2. Should I use :FireAllClients()?
  3. Should effects like shockwaves or other physical effects be played locally?

Yes, everything can be done locally like that. It’s better this way too as it’s far smoother and more accurate than letting the server handle it. But it is harder to setup and damage still won’t be synced 100% unless you do some fancy maths.

For FireAllClients(), it kind of depends on how you’re managing enemies. It might be smarter to only fire to nearby clients though, or whichever ones are engaged in combat with it. Another challenge is syncing the client render vs the server enemy, so you’ll probably have to give each enemy a unique identifier of some kind when communicating so the client and server can tell things apart.

It took me a long period of time, but I was able to get all enemy animations played locally and not on the server, and all the effects are played local to reduce major stress to the server. Super smooth animations, and the greatest benefit to this is that effects can be locally controlled, which means if I could add a setting to have lower quality, those effects can be disabled easily without a lot of server checks!

1 Like

Nice, I hope the rest of your project turns out well :+1:

Good idea with the settings as well.