Entity Get Player Owner

Hello, I’m trying to make a ID Card but when I try “self:GetOwner()” it say’s NULL so, how can I get the owner and take information of him like his name Nick ?

Thx for answers :stuck_out_tongue:

The old wiki states that GetOwner will only return what was set by SetOwner, so you will have to call self:SetOwner() with the user who spawned it.

You may also be able to use self.Owner or self.Player, however these are unlikely to work unless set manually also.

I put it in ENT:Initialize ?

ply:Name()

Not sure if that’s what you’re looking for.

I’m looking to set the owner of the entity as the player who spawned it.

It depends on how you spawn it. If you spawn it with the q menu, then it SHOULD automatically set ent.Player to the create. If you spawn it manually with ents.Create() then you need to set it yourself.

I have tried “idcardspawn:SetPlayer(ply)” in the command which spawn the entity and “self.Player:Nick()” in the entity but it says “attempt to index field “player” a nil value”

I thought you were using Set and Get Owner? AFAIK SetPlayer() isn’t a function on entities.



function SpawnEnt(spawner)
     local ent = ents.Create("some class")
     ent:SetOwner(spawner)
end

-- Later you want to get the owner, and I assume you already have gotten the entity as "ent"
    local owner = ent:GetOwner()


I don’t really know how the Get and Set Owner system works, so if you only need to access this on ents that you spawn, you could just as easily set any field on the entity and access that later.

I have this chat command :


local position = ply:GetPos()
local idcardspawn = ents.Create("id_card")
		local random = math.random(0,1)
	if random == 1 then
		idcardspawn:SetModel("models/dorado/tarjeta"..math.random(1,4)..".mdl")
	else 
		idcardspawn:SetModel("models/dorado/tarjetazero.mdl")
	end 
		idcardspawn:SetOwner( ply )
		idcardspawn.nodupe = true
		idcardspawn:SetPos( position )
		idcardspawn:Spawn()

And this in the ENT:Use() :


function ENT:Use(activator, caller)
	local owner = self:GetOwner()
	net.Start("ouvrir_choix_menu_id_card")
		net.WriteString(owner:GetModel())
		net.WriteString(owner:Nick())
		net.WriteString(owner:GetPData("cid", "rien"))
		net.WriteString(owner:GetPData(pointDeLoyaute, "rien"))
		net.WriteString(owner:getDarkRPVar( "job" ))
		net.WriteString(owner:GetPData(self.Player:SteamID().."description", "rien"))
		net.WriteString(owner:GetPData(self.Player:SteamID().."caractere", "rien"))
		net.WriteString(owner:GetPData(self.Player:SteamID().."date_de_naissance", "01/01/0001"))
		net.WriteString(owner:GetPData(self.Player:SteamID().."race", "rien"))
		net.WriteString(owner:GetPData(self.Player:SteamID().."profession", "rien"))
	net.Send(caller)
	
end

When I spawn with the command the entity, and i press use on it, it opens nothing.

What is it supposed to open? That just sends a net message to whoever used it. Do you have client code that displays that information?

Have you tried debugging? Make sure :USE is being called, make sure owner is valid, etc. Make sure the client is receiving the net message properly.

the Net.Receive is in another file.

Okay… But have you tried debugging as I suggested above? Can we see the net.Receive as well?

There is just vgui in the net.Receive with net.ReadString