NPC Logic on Client vs Server

Hi, Basically I have zombie NPC’s that is spawned and chase the player. At first I was using them on server (I am just a beginner so I learn by trying so I didn’t code those aware of client/server optimization side of things).

In server when I spawn like 120 of them RECV was 100 kb/s around that and every game I play RECV is always low and it should be low as far as I know.

Then I move the AI logic and network ownership of the zombies to player, server only spawns zombies to workspace and RECV is like 4 kb/s with that but now the problem is exploitation. since player owns the zombies I guess they can move the zombies wherever they want.

When I asked this to Gemini (AI) it said normally there is only one basic transparent part that moves with server side AI logic. And client deals with humanoid R6 that kinda stuff. It seems logical but I am not sure if it would have serious effect on optimization since the logic will be in server anyways

Move logic is basically raycasting if zombies see the player and pathfinding when they are not. Also I’m computing each zombies distance to center of the horde so they seems like good horde so I guess it’s kinda costly to performance. I didn’t check microprofiler Idk about this things.

I wonder if anyone dealt with this kinda problem before and would creating that basic part server side logic be better optimization wise

I am aware autistically taking measures against exploitation can be harmful to performance Idk how % of players trynna cheat so either I am gonna renounce performance or security

1 Like

Ooh NPC replication! Love this topic.

If you want to keep NPCs on the server, you can parent them to the server’s workspace.CurrentCamera (quite cursed I know), which prevents automatic replication to clients, then you can implement your own custom replication.

You can go into all sorts of compression, interpolation, grouping, etc!

I recommend taking a look at some buffer networking modules in Community Resources and finding one that fits your case best, or learn how they work and write your own!

1 Like

If you’re adamant to sticking with humanoids, you can try disabling HumanoidStates that you wouldn’t necessarily need.

You can also attempt to hide your NPCs whenever they’re too far, keep in mind that occlusion culling is a thing.

…nevermind, occlusion culling currently doesn’t work for avatars (which I’m assuming means humanoids)


However, there is another method that is commonly used for real-time strategy (RTS) games that involve a buuunch of units – it’s where you ditch serversided humanoids, and instead opt to have the server…

  • figure out the positions of each NPC (and their pathfinding)
    – you can look into modules like FastFlow which use the concept of “flowfields” to avoid pathfinding for each individual NPCs
  • send this position data to clients
    – be smart about how often you send data, “too frequent” can be really laggy – especially with a ton of NPCs. You can look into modules like BridgeNet2 or ByteNet which can really lessen your bandwidth

and the client…

  • render the NPCs and handle all the magic (animations, sounds, etc.)
    – since you’d be loading animations on the client, you can also look into workspace.ClientAnimatorThrottling which throttles animations depending on their distance

There are a ton of genuinely helpful resources for this method, definitely look into these:

1 Like

Occlussion culling has already launched! Though it doesn’t help with OP’s case as it’s a rendering optimization (and also doesn’t work on characters D:), doesn’t help with replication.

You can also try using Instance streaming!

1 Like

I don’t want to keep on server, I just asking which one would be better. I kinda see some networking module from Suphi let me research this networking thing a little.

But do you think it would be possible to decrease it too much because of networking thing. It just decreased 120 to 4 with client.

I am not sure if I should work on anti cheat with client npc or networking optimization with server npc

If you need security, you should keep it on the server and apply the optimization techniques, but if you want a “Get What You See” experience where the client handles most of the gameplay, then you can have it on the client.

1 Like

OHHH REALLYY? Bummer… The original logic for it was to limit the amount of NPCs rendered – but that’s interesting.

I’m not sure if characters can be culled or if they’re incapable of culling other objects.
Yep, they indeed can’t be culled (sad).

1 Like

Appreciate the long response I tried those generally except networking thing. I researched fastflow but it’s not fit to my game I guess I didn’t need it. The game is like in arena setting so zombies %99 of the time use raycasting to player, if they see they go which arena is pretty empty so they see.

I don’t know how much raycasting cost the system but it’s better than pathfinding ig. Will look into Networking thing, Have u tried basic part server logic with client humanoids?

I suppose you could group your raycasts (wherein you can setup a single raycast for a group of NPCs) and try limiting their rate.

1 Like

You don’t have to ditch server Humanoids, there’s some neat tricks. Like I mentioned, you can have fully simulated instances parented to the server’s camera, which is not visible to clients.

Interesting I’ve never thought about that and it seems like a great idea…

I still don’t understand why would I want not visible. They are zombies they should chase player

You might also want to consider looking into Parallel Luau, it’s kind of niche but allows you to implement multi-threading.

2 Likes

By having them not visible to the client, you prevent the engine from replicating the instances automatically, meaning you can implement your own replication while keeping everything on the server the same.

1 Like

Appreciate it, I’ve heard about it let me look at it

Genuinely curious, is there a benefit to parenting them to CurrentCamera specifically and not something like ServerStorage?

And also, what do you mean by “custom replication”? Would you only replicate NPCs that are close to players?

1 Like

CurrentCamera is in workspace, meaning the NPCs are actually simulated!

It means instead of letting the engine handle replication of the instances and properties, you use remotes to handle it, which gives you precise control over the entire process.

1 Like

OHHH THATS GENUINELY SOO COOOOLL, thanks! Here’s me testing that actually:

1 Like