Entity Help

Hi, I am coding an entity that spawns as a wardrobe and when they press e it changes their playermodel…I was wanting to know if anyone knows how to make it so that only the player that spawned the entity can press e and use it.

Try it!


cl_init.lua


include('shared.lua')
ENT.RenderGroup = RENDERGROUP_BOTH
function ENT:Draw()
	self.Entity:DrawModel()
end

**Init.lua
**




function ENT:Initialize()
	self:SetModel( "models/props_junk/propane_tank001a.mdl" )
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:SetUseType( SIMPLE_USE )
	local phys = self:GetPhysicsObject()
	phys:Wake()
end


function ENT:Use( activator, caller )
     ply:SetModel( "models/player/suits/male_07_closed_tie.mdl" ) -- PLY ISN'T DEFINED, USE activator:SetModel() 
     Entity:Remove() -- ONCE AGAIN, CHANGE ENTITY TO self:Remove()
end

shared.lua



ENT.Type 			= "anim"
ENT.Base 			= "base_gmodentity"
ENT.PrintName = "Gas"
ENT.Author = ""
ENT.Category = "Cook"
ENT.Spawnable = true
ENT.AdminSpawnable = true

Player models List :
Click Me

Can anyone else help me with this ?>

This worked however it did not restrict it to the player who spawned the entity

Well a simple naive approach that comes into my mind is to set the owner of the entity upon spawn and then check if the activating player equals the owner.

How do I set it up for the entity to be assigned an owner ?

I tried if activator == caller then
activator:SetModel( “models/player/harry_potter.mdl” )
self:Remove() else
return false
end
end

Did not work

if

Entity:GetCreator() == activator then <do stuff> end

Now it does not let anyone use it…not even me

It’s not even showing up in my entities tab now after using your code, here is what my init.lua looks like


AddCSLuaFile('cl_init.lua')
AddCSLuaFile('shared.lua')
include('shared.lua')

function ENT:Initialize()
	self:SetModel( "models/props_junk/propane_tank001a.mdl" )
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:SetUseType( SIMPLE_USE )
	local phys = self:GetPhysicsObject()
	phys:Wake()
end

hook.Add( "PlayerSpawnedSENT", "SetEntityOwner", function( ply, ent ) )
	ent:SetOwner( ply )
end


function ENT:Use( activator, caller )
   if ent:GetOwner() == caller then
     activator:SetModel( "models/player/harry_potter.mdl" )
     self:Remove()
 end
end

Yes, and an admin spawnable.

Yep

[editline]28th February 2017[/editline]

it also does this when the code breaks in the init.

DO NOT USE SETOWNER! It’s not what you think.

If anything, use SetCreator and GetCreator.

You guys were getting me to use ent:…I can’t use that inside the ENT:Use function as it uses only activator and caller variables…

replace ent with self, self is your entity

[editline]28th February 2017[/editline]

also, activator is the player, not caller

so should I use galaxies hook but replace it with self…and replace getowner with Getcreator in the Ent:Use function ?

[editline]28th February 2017[/editline]

Never mind, your code snippet worked all along lol…just forgot to put self rather than ent. Thanks a ton