GetActiveWeapon():GetClass() == "Pocket" then - Getting Null Entity?


ERROR: Hook 'InvPlyUse' Failed: [@addons\cyder roleplay addons\lua\autorun
server\InvServer.lua:190] Tried to use a NULL entity!

Line 190 is:

[lua]if ply:GetActiveWeapon():GetClass() == “pocket” and ply:KeyDown(IN_ATTACK) then[/lua]

What’s being called that’s ‘null’ ?

Further more, how do I fix it?

Try adding [lua]ply:GetActiveWeapon() and ply:GetActiveWeapon():IsValid()[/lua] before you’re trying to get the class

You are trying to call GetClass() on a NULL entity, meaning if the player doens’t have a weapon it will error as NULL entity.

So it would be…
[lua]if ply:GetActiveWeapon() and ply:GetActiveWeapon():IsValid and ply:GetActiveWeapon():GetClass() == “pocket” and ply:KeyDown(IN_ATTACK) then[/lua]

You forgot the brackets after IsValid, its a function, you want to use it, not pass it as a object.

Oh, sorry, was a quick copy and paste. Thanks alot :)!

I often use **[G.ValidEntity

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=G.ValidEntity)** (or IsValid( entity )) to replace the nil and entity:IsValid() check combo. Improves code readability.

so it’d do it like
[lua]local wep = ply:GetActiveWeapon()
if ValidEntity(wep) and wep:GetClass() == “pocket” and ply:KeyDown(IN_ATTACK) then[/lua]

Trivial, but it’s good practice to do these things in case you ever come back to your code a few months later.