Entity Owners

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

Simple way is to,

**[Entity.SetOwner

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Entity.SetOwner)**

But suggest using a var like,

**[Entity.SetNetworkedInt

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Entity.SetNetworkedInt)**

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?

[lua]

hook.Add( “PlayerSpawnedProp”, “SetPropOwner”, function( p, m, e )
return e:SetNWInt(“Owner”, p:SteamID())
end)
[/lua]

[lua]
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
[/lua]

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?

no

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 "  )
		
end

[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:
[lua]
function ENT:SetupDataTables()
self:DTVar(“Entity”,0,“owner”) --I think it works like this. Might be the other way around.
end
[/lua]

And then in your ‘buying’ script:
[lua]
ent:SetDTEntity(“owner”,consumer)
[/lua]

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

Thanks i got it!