Handling 200+ NPCS without lag

So I have around 200+ NPCs that spawn into my game and they all each have a FollowScript, HealthScript and a AttackScript. The FollowScript is causing the most lag and im not sure how I would optimize the game. Any tips would help thanks!

2 Likes

You don’t have to place a script inside each NPC; using CollectionService, you can tag all the objects and run a function on them.

local CollectionService = game:GetService("CollectionService")

local function YourFunction(Object) -- Object will be the NPC when we call it

end

for _, TaggedInstance in pairs(CollectionService:GetTagged("NPC")) do
    YourFunction(TaggedInstance)
end

Let me know if you’ve got any other questions!

I will try this and let you know how it goes thanks! :slight_smile:

1 Like

Try running each follow script at a lower update rate (longer wait in loop). This will make them less responsive, but will cause less lag.

You can also try using parallel Lua. I’m not that familiar with it, but from what I’ve heard it can help.

1 Like

You can handle all your npc at a time with only one script in SSS and one loop.

You just have to do something like this:

While task.wait(0.1)do
   for _,Npc in pairs(workspace.NpcFolder:GetChildren())do
      —Follow code
   end
end

Then you can do the same with all other scripts which will increase a lot game performances.

Also if your npc doesn’t have the same range, damage ect… you can add a « Configuration » folder in each character, which contain all the npc settings, then use it do adapt your code depending of the npc ^^

Yeah this might be problem since im not familiar with CollectionService, but all NPC names are different (essentially a random ID code) which makes it hard to tag? (Im not sure)

Ill try this and let you know thanks!

Ive just tried this and its more convenient but its using relatively the same memory usage

No worries! I should have explained further, you can use a plugin called Tag Editor to tag instances in your game. Just take a look for it!

Would this work tho since im generating mobs via script

Scripts doesn’t affect Memory Usage that much overall exept if there is hundred of infinity loops scripts…

Having a huge memory usage is mostly caused by the map building (Amount of parts, mesh tris count, moving parts, collisions, textures ect…).

Also if you’re checking the memory usage in studio, it is basicaly a lot highter than in game because it handle all studio features.
The base memory usage in studio is around 1980MB while it is only 460 in game.

1 Like

Oh okay thanks for the info didnt know that! :slight_smile:

Okay i’ve found a way around the issue and made a rendering system to load mobs that are needed and mobs which are not needed. Solution is if the a player isn’t within a certain area of the mob then we shouldn’t need to activate the mob (essentially destroy it)

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