Ammo HUD : attempt to call method 'GetActiveWeapon' (a nil value)
14 replies, posted
I want to create an Ammo HUD but every times, i get this error :
http://image.noelshack.com/fichiers/2018/18/3/1525264368-telechargement.png
there is the code :
http://image.noelshack.com/fichiers/2018/18/3/1525264488-screenshot-1.jpg
I've already search in Google, how to but still have this problem.
Thanks for helping me!
Tetrafox.
Client is not defined in your code.
If you see, Its not the problem, because above, I've 100 more lines of code and client is defined :
http://image.noelshack.com/fichiers/2018/18/3/1525266680-screenshot-2.jpg
And in the console, its not written if client is a problem
Check for weapon existance first with IsValid
if (wep ~= NULL) then
--> Your code
return
end
Even better is
if wep then --> Your code return end
Why are you using LocalPlayer instead of client on line 116 if you defined it previously, then?
You didn't include the definition in the original screenshot, that's why I said it. But even the 2nd screenshot doesn't tell us anything.
I tried to run "GetActiveWeapon" on NULL and got the same error. My bet is that you're defining "client" outside the hook. LocalPlayer() returns NULL before the GM:InitPostEntity.
https://i.imgur.com/WDPlehr.png
Yes i'm defining local var outside hook
As I said previously, LocalPlayer() returns NULL before the GM:InitPostEntity. So basically you're trying to get and store a localplayer when it doesn't even exist. I would just declare it inside the hook. I don't think there's a difference perfomance-wise.
Also it's essential to understand the errors you get, in my opinion. You just have to read it carefully to understand where the problem lies.
"attempt to call method GetActiveWeapon (a nil value)" - GMod attempts to call method named "GetActiveWeapon", it appears to be "nil". In your context, it means that "client.GetActiveWeapon" is nil. So the problem is clearly with "client".
And you really should check if entity that is returned from "GetActiveWeapon" is not NULL like people suggested you to do in this thread.
Here...
https://gist.github.com/JohnsonMcBig/3ae8b506a23184363b7856ba89d6b91f
Used to run into the same problems when I didn't understand the way programming languages work in general, don't worry it'll eventually click.
https://i.imgur.com/oBdvBW9.png
I think you meant "IsValid(weapon)" in the second case. Also the first check is useless. HUDPaint doesn't get called when LocalPlayer() is NULL, so "client" will always be a valid entity.
Thanks you soo much!
At the beginning of the Ammo HUD part I put this and it worked: if (client:GetActiveWeapon() != NULL) then
HUDPaint doesn't get called when LocalPlayer() is NULL, so "ply" will always be a valid entity.
It does for at least one frame AFAIK, I remember getting errors from it being NULL for a short time on startup.
There's no reason for it to run when local player isn't loaded. I just tested this on listen server, but I think it behaves the same way on dedicated:
https://i.imgur.com/X0s4GT4.png
https://i.imgur.com/nZlKVb0.png
Sorry, you need to Log In to post a reply to this thread.