How would I make server sided npcs only visible on the client?

Hey, I want to make an RPG game where players fight against enemies. I want the npcs to be visible only to the player who spawned them. However, I want the game logic to run on the server to prevent exploiters from modifying things like health, etc. I’ve started thinking about how to achieve this.

Here are my ideas so far:

  1. → I would create a table for each player, and in that table, I would store all their NPCs. For each NPC in the table, I would store its current location and health. Then, I’d use a .Heartbeat event on the server to loop through all the NPCs in each player’s table and move them toward the player’s character using .Unit. The server would then send the updated position to the corresponding client and set the CFrame of each NPC to the updated position. The npcs would be basically just a table.

I haven’t started scripting this yet, just brainstorming ways to make it work while keeping the script exploit-free, optimized, and efficient.

Any ideas or suggestions are greatly appreciated!

Try using remote events, you can clone one of your NPCs from replicated Storage and set the position on the client, maybe using :FireClient can help this approach.

Hm. I’ll need the positions saved on the server in the future, that’s why I didn’t choose this approach (cloning npcs on server then just moving them on the client). Instead I chose the table approach where I would just create a table like this:

local NPCs = {
[1234567] = { – UserId
[1] = {
Position = …,
HP = …,
}
}
}

Then i’d just connect a .Hearbeat event on the server, loop through every userid in that table NPCs, loop through every npc → update their positions (move it to the player using .Unit etc.) and then firing the table to the player using remote event. On client it will just set the cframe of the npc to that position. I don’t know if this approach is good tho - it’s exploit-free however I don’t know if it will be optimized very well.