Most efficient way to have semi-large amount of NPCs

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

Hey so I want to bump this thread up and see how you guys are handling your NPCs movement and physic calculations.

  • I use Part.CFrame
  • I use BodyObjects
  • I use Part.Velocity

0 voters

If you use Part.Velocity or BodyObjects, how are you handling your NPC going up inclines and steps?

1 Like

Unfortunately I have a game in which I need hundreds of NPCs. It is an RTS style game. What I found is that just having hundreds of humanoids slows down my hefty 1.5k computer (NVMe, gaming ram, 1050 card, 7th gen I7). Looking at the profiler I found that it is the physics updates. What I’m going to have to do is make the NPCs be local to each client, anchored or can collide = false, no humanoid (maybe a AnimationController), and send updates only when required like when they start following a new path. Each client can then update the NPC’s CFrame as often as their frame rate allows using a simple start time and velocity along a path of points. What is nice about NPCs is that they only move where you tell them, so you don’t need to worry about them walking through walls as long as you don’t tell them to. xD But if you were to do this for regular player humanoids, you would need collision detection for walls and other obstacles.

1 Like

If i’m not using default humanoids (like a heathen, apparently) I’ll use body movers.

The way I got a ton of NPCs was by creating a single part on the server to represent a AI entity. Rather than using bodyObjects, I Update their CFrames every 0.25 seconds (running through a table of entities). Using Pathfinding, I just CFramed the entities to each point while the client associates a character model (no humanoid and using animation controller) then tweens to the part associated with the model. Syncing the CFrame intervals along with the Tweening intervals enabled some very smooth results. Not to forget, it allowed me to reach some high npc counts (more than 125 running with impressive results). I use :GetPropertyChangedSignal() a ton with this, perhaps this would be of use?

10 Likes