How to spawn ent only target can see?

Hey there,

Im working on dark rp class in which the player is slender man. I’m trying to create a SWEP that will make slendy appear in front of a victim/target and be seen by no one else. However, I’m currently stuck due to my lack of knowledge on how clients and servers interact. I have the general just but im struggling to understand when a client side code is being executed and when server side code is being executed and how the two work together.

This is my attempt but you’ll see that i thought parenting would attach an ent to its target client so that it would only be rendered for him
I thought about it as well as tested some thing and found out thats not how it works.


 
if SERVER then -- This is where the init.lua stuff goes.

	AddCSLuaFile ("shared.lua")
 
	SWEP.Weight = 1
 
	SWEP.AutoSwitchTo = false
	SWEP.AutoSwitchFrom = false
 
elseif CLIENT then -- This is where the cl_init.lua stuff goes
 
	--The name of the SWep, as appears in the weapons tab in the spawn menu(Q Menu)
	SWEP.PrintName = "Slender SWEP"
 
	--Sets the position of the weapon in the switching menu 
	--(appears when you use the scroll wheel or keys 1-6 by default)
	SWEP.Slot = 4
	SWEP.SlotPos = 1
 
	--Sets drawing the ammuntion levels for this weapon
	SWEP.DrawAmmo = false
 
	--Sets the drawing of the crosshair when this weapon is deployed
	SWEP.DrawCrosshair = false
 
 
	language.Add("Undone_Thrown_SWEP_Entity","Undone Thrown SWEP Entity")
end

SWEP.Author = "gononono64"
SWEP.Contact = " "
SWEP.Purpose = "This SWEP is for the slender class."
SWEP.Instructions = "Left click uncloak for a ply. "
 
--The category that you SWep will be shown in, in the Spawn (Q) Menu 
--(This can be anything, GMod will create the categories for you)
SWEP.Category = "Slender"
 
SWEP.Spawnable = true -- Whether regular players can see it
SWEP.AdminSpawnable = true -- Whether Admins/Super Admins can see it
 
SWEP.ViewModel = "models/weapons/v_hands.mdl" -- This is the model used for clients to see in first person.
SWEP.WorldModel = "" -- This is the model shown to all other clients and in third-person.
 
 

SWEP.Primary.ClipSize = -1
 
SWEP.Primary.DefaultClip = -1
 

SWEP.Primary.Automatic = false
 

SWEP.Primary.Ammo = "none"
 
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"
 
--When the script loads, the sound ''Metal.SawbladeStick'' will be precached, 

local ShootSound = Sound("Metal.SawbladeStick")

function GhostSlendy( ply, target )
	local c_Model = ents.CreateClientProp()
	c_Model:SetPos( ply:GetPos() )
	c_Model:SetModel( "models/props_borealis/bluebarrel001.mdl" )
	c_Model:SetParent( target ) --this doesnt do what i was hoping it would do :(
	c_Model:Spawn()

end
 
function SWEP:Reload()
end
 
function SWEP:Think()
end
 
 
function SWEP:Scare ()
	--Get an eye trace. This basically draws an invisible line from
	--the players eye. 
	local ply = self.Owner
	local tr = ply:GetEyeTrace()
	local target = nil
 	
 	if  (tr.Entity:IsPlayer())then
 		target = tr.Entity
 		target:EmitSound(ShootSound) --will play spooky noises
 		GhostSlendy(ply, target)
 	else
 		GhostSlendy(ply, ply)
 	end
	
		
		 
 
	

end
 
 
-- primary attack
function SWEP:PrimaryAttack()
	
	self:Scare()
end
 
--secondary attack
function SWEP:SecondaryAttack()
	
	self:Scare()
end

A lot of this is copy pasta. Sorry I’m just learning
If anyone can help me better understand this I would really appreciate it.

Thank you

CreateClientsideProp()?

It would probably be a lot easier to make the person who is slender man only visible to the person they make their target. So you can stand in front of that player and then make them the target so they see you

This is what i worked to, but now im stuck. I still feel like im missing something. Initialize function is called when SWEP is created right? so why does this not work?


 
if SERVER then -- This is where the init.lua stuff goes.

	AddCSLuaFile ("shared.lua")
	util.AddNetworkString("SLENDY_SWEPInit")

	util.AddNetworkString("SLENDY_Scare")
	util.AddNetworkString("SLENDY_Scare_Target_ClientSend")
	util.AddNetworkString("SLENDY_Scare_LocalPly_ClientSend")

	local ShootSound = Sound("Metal.SawbladeStick") --precaching sound
 
	SWEP.Weight = 1
 
	SWEP.AutoSwitchTo = false
	SWEP.AutoSwitchFrom = false


---Receiving target from local player for primary
	net.Receive("SLENDY_Scare" ,function (len, ply)
		local target = net.ReadEntity()
		print(target:GetName() .. " executing")
		if target == ply then 
			print("Target is you")
		else
			
				net.Start("SLENDY_Scare_Target_ClientSend")
				net.WriteEntity(ply)
				net.Send(target)

				net.Start("SLENDY_Scare_LocalPly_ClientSend")
				net.Send(ply)
		end


	end)

	net.Receive("SLENDY_SWEPInit",function (len, ply)
		print("worked") -- -------------------------------------------<-<-<-<-<-<-<-<-<-<-<-<-<-------------------------- its not working :(

		local cloakColor = Color(0,0,0,0)
		ply:SetColor(cloakColor)
		ply:SetRenderMode(RENDERMODE_TRANSALPHA)
		end)


elseif CLIENT then -- This is where the cl_init.lua stuff goes
 
	
	SWEP.PrintName = "Slender SWEP" -- q menu name
 
	--Sets the position of the weapon in the switching menu 
	--(appears when you use the scroll wheel or keys 1-6 by default)
	SWEP.Slot = 4
	SWEP.SlotPos = 1
 
	--Sets drawing the ammuntion levels for this weapon
	SWEP.DrawAmmo = false
 
	--Sets the drawing of the crosshair when this weapon is deployed
	SWEP.DrawCrosshair = false

	function SWEP:SendTargetToServ() -- SendTargetToServ function

		--Get an eye trace. This basically draws an invisible line from
	    --the players eye. 
		local ply = self.Owner
		local tr = ply:GetEyeTrace()
		local target = nil
 	
 		if  (tr.Entity:IsPlayer())then 			
 			target = tr.Entity
 		--target:EmitSound(ShootSound) --will play spooky noises
 		else
 				target = ply
 		end


		net.Start("SLENDY_Scare")
		net.WriteEntity(target)
		net.SendToServer()

	end --endfunc



	function SWEP:Reload()

	end
	
	function SWEP:Think()
	end
	 
	 
	 
	-- primary attack
	function SWEP:PrimaryAttack()
	
		self:SendTargetToServ()
	end
	 
	--secondary attack
	function SWEP:SecondaryAttack()
	
		self:SendTargetToServ()
	end

	
	function SWEP:Initialize()		--<-<-<-<-<-<-<-<-<--<-<-<-<<-<-<-<-<-<-<-<-<-<-<-<-<-<-----------------------------------should this not be called when ent first spawns in?
		local ply = self.GetOwner()
		net.Start("SLENDY_PlayerSpawn")
		net.SendToServer()
	end

	net.Receive("SLENDY_Scare_Target_ClientSend",function()
		local ply = LocalPlayer()	
		local slender = net.ReadEntity()	
		print( ply:GetName() .." and ".. slender:GetName() .. " You See Slender")
		local uncloakColor = Color(255,255,255,255)
		slender:SetRenderMode(RENDERMODE_TRANSALPHA)
		slender:SetColor(uncloakColor)
		

	end)
 	
 	net.Receive("SLENDY_Scare_LocalPly_ClientSend",function()
 		local ply = LocalPlayer()
		
		print( ply:GetName() .. ": You Are Frozen")

	end)
	
end


 
--The category that you SWep will be shown in, in the Spawn (Q) Menu 
--(This can be anything, GMod will create the categories for you)
SWEP.Category = "Slender"
 
SWEP.Spawnable = true -- Whether regular players can see it
SWEP.AdminSpawnable = true -- Whether Admins/Super Admins can see it
 
SWEP.ViewModel = "models/weapons/v_hands.mdl" -- This is the model used for clients to see in first person.
SWEP.WorldModel = "" -- This is the model shown to all other clients and in third-person.
 
 

SWEP.Primary.ClipSize = -1
 
SWEP.Primary.DefaultClip = -1
 
SWEP.Primary.Automatic = false
 

SWEP.Primary.Ammo = "none"
 
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"