1

I ran this code and it gave me an error attempt to index global 'self' (a nil value)

hook.Add( "PostDrawOpaqueRenderables","2d3d", function( )  
    cam.Start3D2D( Vector( self.Entity:GetPos() ), Angle(0, -90, 90), 1 ) 

        surface.SetDrawColor( 255, 0, 255 )
        surface.DrawTexturedRect( 0, 0, 500, 500 )

    cam.End3D2D()

end) 
Nifim
  • 4,758
  • 2
  • 12
  • 31
MrYabloko
  • 31
  • 2
  • 3
    Based on the code provide this doesn't seem to be a context were `self` would be anything but `nil`. What do you expect `self` to be? is this inside another function? – Nifim Jan 09 '20 at 23:04
  • Use `function(self)` instead – lhf Jan 10 '20 at 12:35
  • @lhf Unlikely as `PostDrawOpaqueRenderables` hook does not pass anything that would contain an `Entity` member. I would suggest changing `self.Entity:GetPos()` to e.g. `LocalPlayer():GetPos()` and see if the result is satisfying (also unlikely, but we can't really know the author's intent). – Aki Jan 10 '20 at 16:57

1 Answers1

-1

You can use hook.Entity:GetPos() fix this error.
self is a lua keyword, that appears in the method definition of a table.

OuChenglin
  • 24
  • 2
  • 1
    The definition of `self` you provided in your answer is not correct. `self` is simply an arg passed to a function. it is commonly passed using `:` like in `sometable:method()` which is just syntactic sugar for `sometable.method(sometable)` explained here at the end of function definitions: https://www.lua.org/manual/5.3/manual.html#3.4.11. `self` is also not a keyword https://www.lua.org/manual/5.3/manual.html#3.1 – Nifim Jan 10 '20 at 01:30