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)?
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.
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
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.
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
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.
So before I go into too much detail, tl:dr version is this: take a copy of this place and learn: Some zombie game in the works idk - Roblox
(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. @magnalitemade 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.
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
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