Entity Owners

How would i set the owner of an entity to the person who Spawned it?

Simple way is to,



But suggest using a var like,



How would i use a Var for this?

I updated my post. Setting a var on each entity will cause more networking which could result in lag and will result in higher server cpu and ram usage. But their are trades offs.

Yes i understand u could use these, but i mean how would i name it the the actual player who spawned it?


hook.Add( “PlayerSpawnedProp”, “SetPropOwner”, function( p, m, e )
return e:SetNWInt(“Owner”, p:SteamID())

lua_run for k, v in pairs(ents.GetAll()) do if e:GetClass(“prop_physics”) then if e:GetNWInt(“Owner”) != nil then print(e:GetNWInt(“Owner”)) end end end

Where should these codes be placed? I get this.

[gamemodes\project\entities\entities\cheap_printer\init.lua:121] attempt to index global ‘e’ (a nil value)

my bad.

[lua]lua_run for k, v in pairs(ents.GetAll()) do if v:GetClass(“prop_physics”) then if v:GetNWInt(“Owner”) != nil then print(v:GetNWInt(“Owner”)) end end end[/lua]

Do these both go into my entities init.lua?


lua_run is a command.

A steamid is not an int, nor is using the steamid a good idea for setting an owner.

Use the entity object instead.

This is what im trying to do im trying to make it so when someone spawns a printer it makes them owner.

[editline]6th November 2011[/editline]

function ENT:OnRemove()

	owner = v:GetNWInt("Owner")

	Notify(owner, 0, 4, "You have gained "  )

[editline]6th November 2011[/editline]

This is what i currently have.

In DarkRP the entity has the owner already set, if you’re using DarkRP. (you talked about money printers so I just assume.)
Instead of getting the networked integer (which wouldn’t work anyway since you’re doing it very wrong) it should already set the entitie’s owner with entity.dt.owning_ent (entity should obviously be replaced with the entity object)

If he’s not using DarkRP, entity datatables would still be a very good way to do it.

In your ent’s shared.lua:
function ENT:SetupDataTables()
self:DTVar(“Entity”,0,“owner”) --I think it works like this. Might be the other way around.

And then in your ‘buying’ script:

Assuming that consumer is your buyer and “owner” is your variable.

Thanks i got it!