• Primary weapon clip error
    13 replies, posted
Hello world, I have a lil problem here. I solved this problem once, but can't remember how I did it. I have a draw.SimpleText with a code in it that will display the primary / secondary ammo. When the player dies it gets a error (yellow). local AmmoClip = LocalPlayer():GetAmmoCount( LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType() ) local AmmoClip2 = LocalPlayer():GetActiveWeapon():Clip1() draw.SimpleText(AmmoClip, "Text5", 970, 947, Color( 96, 96, 96, 200), TEXT_ALIGN_CENTER ) draw.SimpleText(AmmoClip2, "Text6", 925, 940, Color( 255, 255, 255, 255), TEXT_ALIGN_CENTER ) [ERROR] lua/autorun/client/cl_hud_load.lua:132: attempt to call method 'GetPrimaryAmmoType' (a nil value) 1. v - lua/autorun/client/cl_hud_load.lua:132 2. unknown - lua/includes/modules/hook.lua:84
Player:GetActiveWeapon is probably returning NULL. Check if it returns a valid weapon with IsValid or just by checking that it isn't NULL.
Okay, I understand it a little bit. But not sure what to do now. Im fairly new to coding Lua.
You'd do an if statement and check if the weapon is valid. [code]local wep = Player:GetActiveWeapon() -- no point calling the function more than once -- if the weapons doesn't equal NULL then -- NULL represents a non existent entity. if (wep ~= NULL) then -- prevent rest of the code running return end local store = Player:GetAmmoCount(wep:GetPrimaryAmmoType()) local clip = wep:Clip1() [/code]
Thanks for your help! Only have a other error now. Looks like the same error but it's now calling "Clip1" a nill value. [ERROR] lua/autorun/client/cl_hud_load.lua:133: attempt to call method 'Clip1' (a nil value)   1. v - lua/autorun/client/cl_hud_load.lua:133    2. unknown - lua/includes/modules/hook.lua:84 if (wep ~= NULL) then     draw.SimpleText(store, "Text5", 970, 947, Color( 255, 255, 255, 100), TEXT_ALIGN_CENTER )     draw.SimpleText(clip, "Text6", 925, 940, Color( 255, 255, 255, 255), TEXT_ALIGN_CENTER )     draw.RoundedBox( 0, 945, ScrH() - 130, 3, 15, Color( 255,255,255, 255 ))            return end
You should be getting "store" and "clip" inside the if statement.
Please use wep:IsValid() instead of NULL comparisons - they can be incorrect under certain circumstances.
Under what circumstances can a weapon be neither a weapon or NULL?
Technically none, but NULL comparisons can be incorrect due to how they're pushed to the Lua state. See here: https://github.com/Facepunch/garrysmod-issues/issues/3089
Is Valid checks if the weapon is nil then it checks if the weapon is NULL.
I am not talking about the global IsValid - there is not even a NULL comparrison inside of it. It checks if the value is nil then returns its IsValid method result.
Worked perfect! Only had one more question. How would i set that the text will be only shown when a play has a weapon in his hand, because now it's saying -1 if the players has keys or physgun in their hands.
Only draw the text if the clip is greater than or equal to 0. Ex. if (clip >= 0) then
Thank you for helping me out!
Sorry, you need to Log In to post a reply to this thread.