How to stop lag with many NPC Soldiers?

So um I have a game where there are 2 teams. On the 2 teams there are many warrior nps who will fight each other. Each one has its own script to find an enemy and attack and inside each npc is a weapon with its own scripts. The weapons use .touched events to detect hits. This is needed so they can detect shields being hit and not damage the shield user. The npcs also use roblox animations tracks (not cframe) to attack or walk. Everything is server-sided.

On studio because it runs on my pc the npcs can fight each other although it lags. But when I run it in a real roblox server in game nothing even runs. The npcs are stuck in one place and cant move because of the lag. I am looking for ways to optimise my game. Ive tried making my own humanoid system but it would be too difficult for me and I cant. If you can please give me ways how roblox games tackle this issue. I want to know how games like Ages of Warfare 2 can sustain many npcs without the game lagging so much. Only difference between my game and that game is that mine uses a lot of collisions from .touched events and is very accurate about where the blade hits.

This is what it looks like when I run it in studio:

This is what it looks like when I run it in game:
Unfortunately I cant upload the footage dev forum keeps giving me errors but basically nothing moves. The arrows stay in the air for so long there are like many of them in the air at once. Everything moves so slowly or doesnt move at all. The game cant run.

This is the game btw: Battle Simulator 2 - Roblox

4 Likes

You should Probably Manage your NPC’s with probably at least One Script to Control all them, and what they are ment to do, Instead of having Multiple Scripts to Do Multiple Tasks, Thats a lot of Information being sent at one time which can cause lag, so you should Check the MicroProfiler to see what is causing the Most Issues, and if you want to, use Parallel Lua to help with some Performance.

6 Likes

kind of looks like you have many loops running at the same time, thats probably whats causing the lag cause alot of connect events dont make lag like that

3 Likes

You could try an assortment of techniques. Some for this use case include: animating and creating the physical models of NPCs client sided (so the server only contains their root part and essential data), creating custom humanoids (as such a larger number of humanoids is very expensive), controlling NPCs from one individual script and managing NPCs as collective groups or instead having many parallel scripts - NPCs are one of the reccomend use cases for the parallel programming model.

7 Likes