Most efficient way to have semi-large amount of NPCs

My goal is to have roughly 100 or so NPCs in the game at once.

Right now I’m adding the packages and other visuals on the client, but there’s still a bit of server lag.

Would it help if I combined all the NPC control scripts into one script on the server that just looped through and did the code from there(using a module script and coroutines)?

10 Likes

Sounds like you have a separate script inside every single NPC. Tag them all with something and use CollectionService with only one master script to handle all their behaviour.

13 Likes

What are you using the NPCs for?

Restaurant Tycoon doesn’t have humanoids in the NPCs and that reduces lag by a lot. But it depends what you are using them for

3 Likes

Hostile NPCs

Kinda like zombies

1 Like

Yeah using one script will help a lot. Since that way you will be using less resources. And game has to load one big script instead of 100 small ones(which will have same stuff written in it). Just put those NPCs in array and do for loop

2 Likes

Would it be better to have a module inside each zombie that’s required whenever the zombie loads contains the for-loop?

Or would it be best to just have one while loop on the server and have each zombie run off that one with coroutines?

1 Like

The best way to use a module in my opinion would be only to use it if the NPC had custom data that needed to be used (a custom function for attacking, or stats about the NPC, for example).

Otherwise it’s just bloating your game with unnecessary objects.

In my opinion you should avoid using module scripts

1 Like

That doesn’t affect him at all - it’ll only need to return once (unless he uses an inefficient method).

2 Likes

Currently there’s only one module as a child of the main script where all the main code is contained. I use it to make it easier to edit just that code instead of everything else inside the script

1 Like

Yes, combining all scripts into one should help quite a bit, that’s exactly what I’m going to be doing in my game.

2 Likes

for i , v in pair(NPCTable) do
spawn(function()
end)
end

that is what I would do it make them run at almost the same time

2 Likes

Well I was thinking more of a functions like for example if you want to calculate distance between all zombies and players and based on that change zombies targets. you could do itusing 2 for loops in 1 while loop. like while true do wait() for i=1,#PlayerTable do for r=1,#ZombieTable do local distance =
(PlayerTable[i].UpperTorso.Position-ZombieTable[r].UpperTorso.Position).magnitude end end end. this way you could calculate magnitudes of every zombie and player using one while loop.

1 Like

So before I go into too much detail, tl:dr version is this: take a copy of this place and learn: https://www.roblox.com/games/370366343/Some-zombie-game-in-the-works-idk
(Warning code is messy and extremely outdated.)

For starters I would advise that you learn what OOP is and how to implement it on Roblox, it makes the creation of AIs SO much easier, thankfully. @magnalite made a thread about it in 2014., After that I would highly advise you to dump using Roblox’s Humanoid objects, they’re laggy, easily exploitable and difficult to work with. In the link above I made my own humanoid and physics by the use of tables and raycasting, which proved to take very little work or effort to make and only a fraction of the cost of resources that humanoids used. And most importantly, this will give you a significant boost in performance… You AI on the server only needs to be a single part, as you will see in the place above all of my AI(s) are a single 2x2x1 torso part on the server, while full bodied zombies on the client.

Its really not that hard to run 100 zombies on the server, just make your own humanoid and only use one part on the server to represent the zombie’s body. What will get you is having 100 zombies on the client with animations playing.

36 Likes

Thanks for the reference place! I’ll definitely try to take some of these methods in account.

For curiosity reasons, how would you go about doing it with humanoids?

1 Like

Don’t use humanoids.

Same thing, but rather then making your own :MoveTo() stuff, just use the humanoid’s built in MoveTo.

But you’re not going to be getting 100 humanoids without the cost of 10-20% of server resources. (In my experience)

1 Like

Of course, just do what you need to do just saying if I would do it I would do it that way, but I never done it before so I wasn’t saying all the details

How do you create custom HipHeight? Would you run a loop raycast for each zombie and move them up off the ground?

I was also wondering if this was possible to do on actual players so I don’t have to use any humanoids in my game at all.

If you took a look at the code, you would see I did that. I used a ray to detect ground beneath the NPC, if there’s ground then the NPC would place its rootpart 2.5 studs above the ground.

server side replicates and handles NPC wanted positions, with a task scheduling OOP system for each NPC

client receives wanted NPC positions in a periodic loop, (1 time per second? 1 time per 5 seconds? up to you), and interpolates NPC positions for walking and handles NPC interaction animations

keep server side physics calculations (ie; moving blocks around and replicating humanoid stuff) at minimum, game/block interactions should be handled client side so that the only data being passed is at the bare minimum data

1 Like