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.
[B]Try it![/B]
[CODE]cl_init.lua[/CODE]
[CODE]include('shared.lua')
ENT.RenderGroup = RENDERGROUP_BOTH
function ENT:Draw()
self.Entity:DrawModel()
end[/CODE]
[B]Init.lua
[/B]
[CODE]
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[/CODE]
[B]shared.lua[/B]
[CODE]
ENT.Type = "anim"
ENT.Base = "base_gmodentity"
ENT.PrintName = "Gas"
ENT.Author = ""
ENT.Category = "Cook"
ENT.Spawnable = true
ENT.AdminSpawnable = true[/CODE]
Player models List :
[URL="https://csite.io/tools/gmod-universal-mdl"]Click Me[/URL]
Can anyone else help me with this ?>
[QUOTE=Heroezzz;51875597][B]Try it![/B]
[CODE]cl_init.lua[/CODE]
[CODE]include('shared.lua')
ENT.RenderGroup = RENDERGROUP_BOTH
function ENT:Draw()
self.Entity:DrawModel()
end[/CODE]
[B]Init.lua
[/B]
[CODE]
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[/CODE]
[B]shared.lua[/B]
[CODE]
ENT.Type = "anim"
ENT.Base = "base_gmodentity"
ENT.PrintName = "Gas"
ENT.Author = ""
ENT.Category = "Cook"
ENT.Spawnable = true
ENT.AdminSpawnable = true[/CODE]
Player models List :
[URL="https://csite.io/tools/gmod-universal-mdl"]Click Me[/URL][/QUOTE]
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.
[QUOTE=Failure;51886888]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.[/QUOTE]
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 [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/GetCreator]Entity:GetCreator[/url]() == activator then <do stuff> end
[QUOTE=NeatNit;51887095]if [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/GetCreator]Entity:GetCreator[/url]() == activator then <do stuff> end[/QUOTE]
Now it does not let anyone use it...not even me
Haven't tested it but give it a try.
[code]
-- Set the entity owner
hook.Add( "PlayerSpawnedSENT", "SetEntityOwner", function( ply, ent ) )
ent:SetOwner( ply )
end
-- Get the entity owner
if ent:GetOwner() == caller then
-- Code goes here
end
[/code]
[QUOTE=GalaxyBeatzz;51887171]Haven't tested it but give it a try.
[code]
-- Set the entity owner
hook.Add( "PlayerSpawnedSENT", "SetEntityOwner", function( ply, ent ) )
ent:SetOwner( ply )
end
-- Get the entity owner
if ent:GetOwner() == caller then
-- Code goes here
end
[/code][/QUOTE]
It's not even showing up in my entities tab now after using your code, here is what my init.lua looks like [CODE]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[/CODE]
[QUOTE=BeZerk;51887207]It's not even showing up in my entities tab now after using your code, here is what my init.lua looks like [CODE]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[/CODE][/QUOTE]
In that case have you defined it as spawnable in the shared file?
[QUOTE=GalaxyBeatzz;51887236]In that case have you defined it as spawnable in the shared file?[/QUOTE]
Yes, and an admin spawnable.
[QUOTE=BeZerk;51887305]Yes, and an admin spawnable.[/QUOTE]
Restarted your game and categorized the entity?
[QUOTE=GalaxyBeatzz;51887324]Restarted your game and categorized the entity?[/QUOTE]
Yep
[editline]28th February 2017[/editline]
[QUOTE=GalaxyBeatzz;51887324]Restarted your game and categorized the entity?[/QUOTE]
it also does this when the code breaks in the init.
[QUOTE=BeZerk;51887329]Yep
[editline]28th February 2017[/editline]
it also does this when the code breaks in the init.[/QUOTE]
Sorry I thought you said it never was spawnable.
Which gamemode are you using this for?
[b]DO NOT USE SETOWNER![/b] It's not what you think.
If anything, use SetCreator and GetCreator.
[QUOTE=NeatNit;51887385][b]DO NOT USE SETOWNER![/b] It's not what you think.
If anything, use SetCreator and GetCreator.[/QUOTE]
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
[QUOTE=NeatNit;51887421]replace ent with self, self is your entity
[editline]28th February 2017[/editline]
also, activator is the player, not caller[/QUOTE]
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]
[QUOTE=NeatNit;51887421]replace ent with self, self is your entity
[editline]28th February 2017[/editline]
also, activator is the player, not caller[/QUOTE]
Never mind, your code snippet worked all along lol...just forgot to put self rather than ent. Thanks a ton
Sorry, you need to Log In to post a reply to this thread.