I was wondering how could i make a spawn system for npcs that checks the distance of a localplayer, and spawns the npcs based on that, every npc has a render distance, should i send a remote event?, but how would i optimize this?, A while true loop or a renderstepped?, that’s some of the questions
NPC spawning is probably something that will (at maximum) only happen once every few seconds I assume, therefore binding to renderstepped or stepped/heartbeat seems like overkill. A while wait(x) do
should suffice, and I’d run it on the server and check distances using (Character.HumanoidRootPart.Position -NPCSpawnPosition).Magnitude
.
By ‘every npc has a render distance’ do you just mean every npc will only spawn if they’re a certain distance from a character? Or do you literally mean if they’re in render distance of the player’s camera?
every npc will only spawn if they’re a certain distance from a character?
Yeah, any character, but then i would deal with it despawning if some other condition is met, like :
- The Npc is not near a character for a time, then despawn , etc(?
I had thought on running it on the server but, It wouldn’t affect performance?
‘every npc has a render distance’
Yes, But in this case it’s like the distance from the spawner than the actual npc (when they first spawn), since there will be spawner parts (these already exist in workspace)
I was thinking on doing another methods like, If i do it on the client, i can do GetPropertyChangedSignal(“MoveMagnitude”) and check if the character is near it, then send a remote event (maybe very exploitable) but let me know what you think
I imagine you’re not going to have too many performance issues with spawning serverside compared to clientside in most instances. And if it’s a multiplayer game then you definitely need to spawn on the server otherwise other players won’t be able to see the NPCs.
If it’s a multiplayer game and you’re seriously concerned about performance losses outside of the render distance, and are intending on adding some form of instance streaming, maybe consider enabling StreamingEnabled. There are probably more techniques along the lines of re-parenting or temporarily destroying NPCs on the client if they’re outside render distance, but that would be overkill for most games and there are better people to ask for advice on that as it will take a relatively large amount of work.
“The Npc is not near a character for a time, then despawn , etc(?”
while task.wait(0.5) do -- If you really want to bind it to a stepped function then you can, but that seems unnecessary for simple de-spawning
local Position1 = Character.HumanoidRootPart.Position
local Position2 = NPC.HumanoidRootPart.Position
local Distance = (Position1 -Position2).Magnitude
if (Distance > MaximumNPCDistance) then
NPC:Destroy()
end
end
Obviously if you have more than one player, or more than one NPC then you’ll have to loop through them. If you have multiple players, then you’ll have to check distance from every single one to verify that no-one is within range before destroying,