Having problem making an entity that gives you a weapon then is removed
4 replies, posted
Well I am trying to create a very basic DarkRP addon that can be used to turn 'scrap' into 'refined' which can be picked up to be used as a weapon. The weapon the 'refined' is supposed to give you before being removed is weapon_crowbar. (I know I'm working out the names and models.)
So far [I]most[/I] of the hard work is already done. However I am having a bit of trouble.
[CODE]AddCSLuaFile("shared.lua")
include("shared.lua")
function ENT:Initialize()
self:SetModel( "models/props_canal/mattpipe.mdl" )
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
self.isRunning = false
local phys = self:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
end
end
function ENT:Use(a , c)
self:Give( "weapon_crowbar" ) then
self:Remove()
end[/CODE]
I'll appreciate any help as I am a total noob compared to those who have been doing addons for years.
it would help if you'd explain what went wrong instead of just saying "it has a problem"
[editline]8th January 2017[/editline]
i'm not going to plug the code into gmod to find out, but notably you wrote self:Give instead of a:Give
you're attempting to give the entity that's being removed a crowbar, instead of giving the player using the entity a crowbar
[editline]8th January 2017[/editline]
or is it c:Give? i'm not sure anymore, but it isn't self
[QUOTE=bitches;51642387]it would help if you'd explain what went wrong instead of just saying "it has a problem"
[editline]8th January 2017[/editline]
i'm not going to plug the code into gmod to find out, but notably you wrote self:Give instead of a:Give
you're attempting to give the entity that's being removed a crowbar, instead of giving the player using the entity a crowbar
[editline]8th January 2017[/editline]
or is it c:Give? i'm not sure anymore, but it isn't self[/QUOTE]
What I am trying to do is create an entity when a player uses it (Presses E on it) the entity is removed and a crowbar is given to them.
Also I keep getting this ERROR: [ERROR] addons/my addon/lua/entities/refined/init.lua:19: unexpected symbol near 'then'
[editline]8th January 2017[/editline]
Holy Jesus I actually learned something! I managed to find the problem. I was missing an 'if' statement! I feel super smart now even though it probably was a pathetically obvious mistake...
[CODE]AddCSLuaFile("shared.lua")
include("shared.lua")
function ENT:Initialize()
self:SetModel( "models/props_canal/mattpipe.mdl" )
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
self.isRunning = false
local phys = self:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
end
end
function ENT:Use(a , c)
if c:Give( "weapon_crowbar" ) then
self:Remove()
end
end[/CODE]
Now how do I make it so if a player already has a crowbar they can't pickup the 'refined'
[CODE][/CODE]
[editline]8th January 2017[/editline]
I am currently trying to figure out out to make it so if a player [U]already has[/U] a crowbar they cannot pick the 'refined' up.
I realize the code is no where near correct, but is anyone able to shed some light on the subject?
[CODE]AddCSLuaFile("shared.lua")
include("shared.lua")
function ENT:Initialize()
self:SetModel( "models/props_canal/mattpipe.mdl" )
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
self.isRunning = false
local phys = self:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
end
end
function ENT:Use(a , c)
if c:Give( "weapon_crowbar" ) then
--if :HasWeapon("weapon_crowbar") ) end
self:Remove()
end
end [/CODE]
[code]function ENT:Use(a , c)
if c:HasWeapon("weapon_crowbar")
return;
end
c:Give( "weapon_crowbar" )
self:Remove()
end[/code]
This will check if they own the crowbar, if they do the function will stop (Due to return), if they don't it continues and gives them a crowbar.
You knew what you needed and even found a function that could help you do it but you didn't know where to put it. Just remember when a function is called (For example Use when the object is used), that function will be executed from top to bottom so you need to wrap your code in statements or add checks to stop it continuing if the requirements arent met.
An alternative and I guess more readable version of the above would be to check if the player [B]doesn't[/B] have the weapon, which you can do by adding a ! to the start of what you are checking.
[code]function ENT:Use(a , c)
if !c:HasWeapon("weapon_crowbar")
c:Give( "weapon_crowbar" )
self:Remove()
end
end[/code]
Also this isn't the section for GLua help, [url=https://facepunch.com/forumdisplay.php?f=65]Garrysmod Developer Discussion[/url] exists for that.
Thanks my dude! :D
Sorry, you need to Log In to post a reply to this thread.