[CODE]
if ( self.Owner():GetActiveWeapon():GetClass() == "joyofpainting") then
[/CODE]
That's the code, and this is the error:
[CODE]
[ERROR] addons/santa/lua/weapons/joyofpainting.lua:589: attempt to index global 'self' (a nil value)
1. unknown - addons/santa/lua/weapons/joyofpainting.lua:589
[ERROR] addons/santa/lua/weapons/joyofpainting.lua:589: attempt to index global 'self' (a nil value)
1. unknown - addons/santa/lua/weapons/joyofpainting.lua:589
[/CODE]
Why isn't self working/What do I replace it with?
Please halp
If you posted the full code then we'd be able to help you much more easily.
From what I can see from that single line is that 'self' isn't defined.
[QUOTE=Moosicorn;50685893]If you posted the full code then we'd be able to help you much more easily.
From what I can see from that single line is that 'self' isn't defined.[/QUOTE]
It is defined, it's a SWEP dude
I've used self.Owner in PrimaryAttack and i've also been able to print self.Owner():GetActiveWeapon():GetClass() successfully but it doesn't work inside IF for some reason and I don't know why
[quote]self.Owner()[/quote]
Try just self.Owner
[QUOTE=NiandraLades;50685916]Try just self.Owner[/QUOTE]
Nope, same error :(
If it was actually defined in a swep, you wouldn't have the error, post the full code so we can help you.
[QUOTE=Apickx;50686032]If it was actually defined in a swep, you wouldn't have the error, post the full code so we can help you.[/QUOTE]
dude
it worked on primaryattack
it's working on the code for the custom weapon viewmodel from swep construction kit
i was able to print self.Owner():GetActiveWeapon():GetClass()
that means it's defined right?
but it's not working on that IF for some reason
If you don't want to post the full code of your weapon, just post the code of the function containing the error (post at least the name of the function e.g. SWEP:OnReload)
Put this on the line above and run the code
[code]
print(self, type(self))
[/code]
Edit: Also, instead of using self.Owner you should be using self:GetOwner(). They should point to the same entity, just the first one is legacy support.
[QUOTE=bigdogmat;50686090]Put this on the line above and run the code
[code]
print(self, type(self))
[/code]
Edit: Also, instead of using self.Owner you should be using self:GetOwner(). They should point to the same entity, just the first one is legacy support.[/QUOTE]
It says
[CODE]
nil nil
[/CODE]
And self:GetOwner() doesn't work either.
:cry:
[QUOTE=Aarone2004;50685910]It is defined, it's a SWEP dude
I've used self.Owner in PrimaryAttack and i've also been able to print self.Owner():GetActiveWeapon():GetClass() successfully but it doesn't work inside IF for some reason and I don't know why[/QUOTE]
If we had the code for at least the function your code is contained in I'd already know the answer to this question: is this if statement you're talking about contained in a method defined on the SWEP, or some local function inside the same lua file?
The reason I'm asking is because if you arbitrarily reference 'self' from somewhere that's not defined as being a table-specific method like SWEP:PrimaryAttack(), it's going to throw an error since there's nothing to reference.
(assuming the custom function is in the same file as your other swep code) If the custom function doesn't start with SWEP:, then "self" won't be a weapon.
No one can give you more info to actually fix the problem unless you show us the function on question. We can't magically know how to fix everyone's problems.
[editline]10th July 2016[/editline]
Also, read this
[url]http://xyproblem.info/[/url]
This is the entire thing I'm trying to do, and it's outside of all functions.
[CODE]
local ourMat = Material( "models/wireframe" )
if(self.Owner:GetActiveWeapon():GetClass()=="joyofpainting") then
hook.Add( "HUDPaint", "example_hook", function()
surface.SetDrawColor( 255, 255, 255, 255 )
surface.SetMaterial( ourMat ) -- If you use Material, cache it!
surface.DrawTexturedRect( 0, 0, 256, 256 )
end )
else
hook.Remove("HUDPaint", "example_hook" )
end
[/CODE]
Yeah I know it has code from GMOD wiki
So basically
If my current SWEP is this one (joyofpainting) =
Draw a texture in the corner of my screen
If it's not, then remove the texture or make it invisible
[QUOTE=Aarone2004;50686376]This is the entire thing I'm trying to do, and it's outside of all functions.
[CODE]
local ourMat = Material( "models/wireframe" )
if(self.Owner:GetActiveWeapon():GetClass()=="joyofpainting") then
hook.Add( "HUDPaint", "example_hook", function()
surface.SetDrawColor( 255, 255, 255, 255 )
surface.SetMaterial( ourMat ) -- If you use Material, cache it!
surface.DrawTexturedRect( 0, 0, 256, 256 )
end )
else
hook.Remove("HUDPaint", "example_hook" )
end
[/CODE]
Yeah I know it has code from GMOD wiki[/QUOTE]
Yeah, you can't use the self reference like this, read my post above.
Instead of self.Owner in this case, try LocalPlayer().
[CODE]
local ourMat = Material( "models/wireframe" )
hook.Add( "HUDPaint", "example_hook", function()
if ( LocalPlayer():GetActiveWeapon():GetClass() == "joyofpainting" ) then
surface.SetDrawColor( 255, 255, 255, 255 )
surface.SetMaterial( ourMat ) -- If you use Material, cache it!
surface.DrawTexturedRect( 0, 0, 256, 256 )
end
end )
[/CODE]
[QUOTE=timz9;50686389]Yeah, you can't use the self reference like this, read my post above.
Instead of self.Owner in this case, try LocalPlayer().[/QUOTE]
[CODE]
[ERROR] addons/santa/lua/weapons/joyofpainting.lua:590: attempt to call global 'LocalPlayer' (a nil value)
1. unknown - addons/santa/lua/weapons/joyofpainting.lua:590
[/CODE]
:ohno:
snip
[QUOTE=Aarone2004;50686376]This is the entire thing I'm trying to do, and it's outside of all functions.
[CODE]
local ourMat = Material( "models/wireframe" )
if(self.Owner:GetActiveWeapon():GetClass()=="joyofpainting") then
hook.Add( "HUDPaint", "example_hook", function()
surface.SetDrawColor( 255, 255, 255, 255 )
surface.SetMaterial( ourMat ) -- If you use Material, cache it!
surface.DrawTexturedRect( 0, 0, 256, 256 )
end )
else
hook.Remove("HUDPaint", "example_hook" )
end
[/CODE]
Yeah I know it has code from GMOD wiki
So basically
If my current SWEP is this one (joyofpainting) =
Draw a texture in the corner of my screen
If it's not, then remove the texture or make it invisible[/QUOTE]
What about if you use
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/WEAPON/DrawHUD]SWEP:DrawHUD[/url]
[QUOTE=gonzalolog;50686423]What about if you use
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/WEAPON/DrawHUD]SWEP:DrawHUD[/url][/QUOTE]
Shit, that should work
[QUOTE=gonzalolog;50686423]What about if you use
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/WEAPON/DrawHUD]SWEP:DrawHUD[/url][/QUOTE]
THANNKS!!
Sorry, you need to Log In to post a reply to this thread.