Question about NPC visibility

I don’ t get this, can you elaborate further?

Once you create the npc server, it’s visible to everyone right? However, if one client deletes the npc, it won’t be visible to them but still visible to other players. You should look into client replication if you don’t understand this.

One possible solution is to have a folder in workspace to contain all NPCs. Once one is added, change the name of the NPC to the player you want to see it.
ex: “NPC_Fusionet”, “NPC_Empereans”.

You can use .ChildAdded on the client to detect when an NPC is added then check if that player’s name is found in the NPC name. If it’s not, simply destroy the NPC, make it invisible, or parent it to nil and they won’t see it.

First of all that’ s very inefficient and as I said vulnerable if we’ re talking about exploits since they can just not make it delete at all or just not chase them.
Second I still don’ t understand how this would work or just why you would do that.

The method I mentioned still allows for server control over the NPC, so it won’t be vulnerable. I think you’re also misunderstanding.

If you create a basic part on the server, every client will see it. If one client deletes the part, they won’t see it. However, all the other clients will still see it because the server has network ownership over it. This is a basic rundown of replication and can be applied to this situation.

I have no idea what these replies are trying to accomplish, but to make the NPC only visible to one client, you can make the NPC invisible on the server, then through a local script, set the NPC’s transparency to 0 whenever that player is targeted.

Here’s an example of making the NPC visible for the client:

local npc = workspace.NPC

for index, child in pairs(npc:GetDescendants()) do
   if child:IsA("BasePart") or child:IsA("Decal") then
      child.Transparency = 0
   end
end

Before executing this code, the client needs to be able to detect when they’re targeted, this can be done by setting an attribute on the player’s character from the server’s side or in this case, from the NPC script. Example:

local target = targetedCharacterHere

target:SetAttribute("Target", true) -- remove this attribute when the character is no longer targeted by setting the attribute to 'nil'

Back in the client script, you can now do if localPlayer.Character:GetAttribute("Target") then to detect when the client is targeted.

What’s the difference of putting local scripts in the starterpack, startergui, and starterplayer?

As for this question, local scripts should be put in StarterPlayer > StarterPlayerScripts in most cases. Put them in StarterCharacterScripts for the script to run every time your character respawns. Local scripts will still run in StarterGui and StarterPack, but those are outdated methods and could cause your scripts to stop working after any Roblox update.

1 Like

But for Guis, we still put local scripts in startergui. We put local scripts in starterpack for tools.

Put a LocalScript in ReplicatedFirst, next, create a RemoteEvent and put it in ReplicatedStorage. Then, in the script where you spawn NPC, fire all clients except the one NPC is targetting. (The event should contain the NPC as a parameter.)
Write in LocalScript:

local RS = game:GetService("ReplicatedStorage")
local event = RS:WaitForChild(your event name)

event.OnClientEvent:Connect(function(npc:Model)
 npc:Destroy()
end)

In Script add:

--start of the script
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local destroyNPCevent = RS:WaitForChild(your event name)
...
--IN THE PART WHERE NPC SPAWNS
for i, plr in ipairs(Players:GetChildren()) do
 if plr ~= player_targetting then --player_targetting is the variable corresponding to the targetted player
  event:FireClient(plr, npc) --npc is the variable reffering to the npc
 end
end