How can I set up NPCs in a voxel game?

I’m working on a voxel game set up in such a way that no instances are created on the server. This is to limit the amount of work the server does, allow for custom render distances on the client (without having to lower graphics quality), among a few other things.

One thing I’ve been having a really hard time figuring out is how to setup NPCs. Since there are no instances generated on the server, I can’t simply create an NPC on the server and change its position there. I’m really not sure how to handle this (nor do I understand how games like Minecraft handle it). Any help would be appreciated.

1 Like

There isn’t really a solution for this but what you can do is use the ‘terrain:WriteVoxels ()’ method to make the npcs stay still i guess

I’m not using Roblox’s smooth terrain, I’m using BasePart instances for my voxels. I assume that there is some solution for this issue because games like Minecraft obviously have NPCs that work online.

Minecraft and Roblox are different.

I don’t think there is a solution for this, you should be creating voxels on the server anyway, client based generation means its easy for exploiters to abuse. No reason to make it on the client.

2 Likes

You could have the npcs generated on the server, just not in a visual form. You could create a table of npcs on the server, save all info about them on the server, and pass npc data off to the client when they’re close to the npc.

Also to address @KdudeDev, I assume they’re not handling everything on the client. They probably just mean not having instances on the server and instead having them in some other data form, which wouldn’t make a difference in terms of exploiting.

2 Likes

Minecraft and Roblox are different.

Yes, though the comparison you should be making is between Minecraft and Roblox games. You’ve failed to list specific differences in how they’re networked that would prevent me from accomplishing what I’ve sought out to do.

you should be creating voxels on the server anyway, client based generation means its easy for exploiters to abuse. No reason to make it on the client.

There are many reasons to create instances on the client. In a voxel game that can have an infinite amounts of blocks, it would be (literally) impossible to instantiate all parts on the server. Additionally, instantiating parts on the server only increases the server overhead, which I need to keep as low as possible. I can list more if you’re legitimately convinced there is “no reason” to instantiate parts on the client. I believe @Elttob’s game, Blox, has a similar setup. Exploitation is not an issue for the reason @prepsure mentioned.

2 Likes

I don’t think I really need to list any differences between Roblox and Minecraft, but I can.

Roblox is scripted with Lua, Minecraft is scripted with Java. Two different languages.

I don’t know the exact inner-workings of Minecraft, but I’m pretty sure that its not the same Server to Client relationship that Roblox has.

You shouldn’t think that just because Minecraft can do it, so can Roblox. Its two different engines, two different languages, nothing functions the exact same.

There are a bunch of ways to do this, it’s tricky to find the solution that works best for your setup.

If the NPC’s need to remain persistent while they are offloaded (no clients nearby), then what I would do is store the minimal amount of necessary information for the NPC on the server. Basically the appearance, position, the “state” that the NPC is in, and any other vital information. You can store this either via tables and update all of the clients using events (possible remote exhaustion and many bytes being sent to the clients rapidly), or with some sort of instance such as folders and values. I would suggest folders and values if there are many of them that need to be updated frequently.

So then, on the server you should be handling all the movement of these NPCs. I assume you have your voxels set up in a three dimensional table in which voxels[x][y][z] is a block with appropriate information. Given this information, you can write an algorithm that allows the NPC to traverse the land without the instances existing on the server. You would then just adjust the NPC’s data “position” to where the NPC should be in the world, along with any state changes that take place.

Now, each client sees this position change and updates the local NPC’s “goal position”, and also the animation based on the current state — getting this part to look good and replicate well will take some tweaking along with latency calculation.

More advice, if these NPC’s don’t need to be incredibly persistent, we can save even more of a load off the server by completely deleting the NPC when they are several chunks away for a certain amount of time. Then, you could create an event when a client enters an area with no NPCs or possibly a new area and then generate them at that moment.

Hope this helps somewhat, it’s not an easy task or a task with one good solution. Let me know if you have any questions!

1 Like