• GetActiveWeapon():GetClass() == "Pocket" then - Getting Null Entity?
    5 replies, posted
[code]ERROR: Hook 'InvPlyUse' Failed: [@addons\cyder roleplay addons\lua\autorun server\InvServer.lua:190] Tried to use a NULL entity![/code] 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.
[QUOTE=leiftiger;31673318]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.[/QUOTE] So it would be.. [lua]if ply:GetActiveWeapon() and ply:GetActiveWeapon():IsValid and ply:GetActiveWeapon():GetClass() == "pocket" and ply:KeyDown(IN_ATTACK) then[/lua]
[QUOTE=Jaastin;31673380]So it would be.. [lua]if ply:GetActiveWeapon() and ply:GetActiveWeapon():IsValid and ply:GetActiveWeapon():GetClass() == "pocket" and ply:KeyDown(IN_ATTACK) then[/lua][/QUOTE] You forgot the brackets after IsValid, its a function, you want to use it, not pass it as a object.
[QUOTE=leiftiger;31673408]You forgot the brackets after IsValid, its a function, you want to use it, not pass it as a object.[/QUOTE] Oh, sorry, was a quick copy and paste. Thanks alot :)!
I often use [b][url=http://wiki.garrysmod.com/?title=G.ValidEntity]G.ValidEntity [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] (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.
Sorry, you need to Log In to post a reply to this thread.