How to check if eye trace/ target hits an entity?


if not self.GetOwner():GetEyeTrace().Entity then return end

I am creating a weapon that needs to check if it’s hit is an entity, but when I use the this code, I get this error message.


[ERROR] gamemodes/terrortown/entities/weapons/weptest.lua:41: Tried to use a NULL entity!
  1. GetOwner - [C]:-1
   2. unknown - gamemodes/terrortown/entities/weapons/weptest.lua:41


I understand that it is trying to find an entity on my eye trace, but I was under the impression that “self.GetOwner():GetEyeTrace().Entity” would just equal nil if an entity wasn’t found in the trace. That’s obviously not the case, however, so I would like to know if there’s an actual/better way to check if the player is aiming at an entity in a SWEP.

[editline]3rd May 2015[/editline]

Or I might be completely wrong, and the NULL entity might be referring to my use of “self.GetOwner()”, which could be the case, but doesn’t make all that much sense because this is in a SWEP, and I did the same thing the previous line and it worked fine. I dunno.

Yes you’re correct, the NULL entity error is happening because you’re using self.GetOwner instead of self:GetOwner, : automatically inserts the object as the first parameter of the function ( in this case self ), . doesn’t.