Cant physgun spawned enitity until I rejoin server

Hey there, Im trying to make an add-on that allows me to spawn in an entity with a variable model.
When I try to physgun the spawned entity I am unable to until i rejoin the server. I do have fpp installed. Any ideas?
Thanks in advance for any replies

Spawn function

function TOOL:SpawnEntity(class, model, pos )	
	if CLIENT then return end

	local ent = ents.Create(class)
	ent:SetModel(model)
	ent:SetPos(pos)
	ent:SetOwner(self:GetOwner())
	ent:Spawn()

	return ent	
end

Entity Initialize

function ENT:Initialize()	
	 --ive messed around with these and it didnt seem to do anything
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetCollisionGroup( COLLISION_GROUP_INTERACTIVE)
	self:SetSolid( SOLID_VPHYSICS  )
	self:PhysicsInit( SOLID_VPHYSICS  ) 
	self:SetUseType( SIMPLE_USE )
    ----------------------------------------------------------------
	self:CPPISetOwner(self:GetOwner())  

	local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
		self:PhysWake()
       end
end

Why use ENTITY.SetOwner on one side and ENTITY.CPPISetOwner on the other? It’s pretty inconsistent I think. You should remove the creator definition in the entity initialization and move SetOwner after the entity spawns. Also, to set the owner of an object, you need to use CPPISetOwner or SetCreator but the SetOwner method isn’t used in this case.

This is what I would do:

function TOOL:SpawnEntity(class, model, position)	
	if CLIENT then return end

	local entity = ents.Create(class)
	entity:SetModel(model)
	entity:SetPos(position)
	entity:Spawn()
	entity:SetCreator(self:GetOwner()) -- Or entity:CPPISetOwner(self:GetOwner())

	return entity
end
1 Like

You are a wizard! I didnt even know about setcreator xD
Thank you so much <3