local Clip1Ammo = Player:GetActiveWeapon():Clip1()
local WeaponName = Player:GetActiveWeapon():GetPrintName()
draw.RoundedBox(0,ScrW - 112,ScrH - 82, 100 + 4 ,30 + 4,Color(40,40,40))
draw.SimpleText(Clip1Ammo,"HUD", ScrW - 62,ScrH - 67, Color(255,255,255),1,1)
draw.SimpleText("","HUD", ScrW - 62,ScrH - 67, Color(255,255,255),1,1)
if Clip1Ammo <= 0 then
draw.RoundedBox(0,ScrW - 112,ScrH - 82, 100 + 4 ,30 + 4,Color(40,40,40))
draw.SimpleText("No Ammo","HUD", ScrW - 62,ScrH - 67, Color(255,255,255),1,1)
end
draw.RoundedBox(0,ScrW - 176,ScrH - 42, 168 ,30 + 4,Color(40,40,40))
draw.SimpleText(WeaponName ,"HUD", ScrW - 93,ScrH - 25, Color(255,255,255),1,1)
This is the piece of code of my HUD, i know its a mess, code structring is not my thing ._.
Everytime i die i get a lua script error, the ammo display works tho, but when i die it dissapears and i get this error
[ERROR] addons/shapedhud/lua/gamemodes/gamemode_sandbox.lua:77: attempt to call method 'Clip1' (a nil value)
1. fn - addons/shapedhud/lua/gamemodes/gamemode_sandbox.lua:77
2. unknown - addons/ulib-v2_63/lua/ulib/shared/hook.lua:109
I know people are saying IsValid() can be helpfull, and if you answer please dont just say "use this or this function"
im a noob pretty much at lua. It wont be helpfull if you just tell me to use this function. Cause i dont know where, what to replace, etc.
Thx for the understanding
Before you call anything on the weapon or draw anything, check if the weapon is valid (Player:GetActiveWeapon():IsValid()).
Ok so i have put my whole ammo and weapon code in a if statement,
if (Player:GetActiveWeapon():IsValid()) then
--HUD CODE
end
Which displays the stuff, but when i die it still makes the error. Which is because (i think) that as soon as its valid it gets displayd, but as soon as i die, it still gets displayd wich ccauses the error, and the thing just get turned on, but never off
Can you post the whole code with the exact error? Also, make sure you've reloaded your server/map after making changes to the code.
I reload everytime yes. Also that is the whole error i get:
[ERROR] addons/shapedhud/lua/gamemodes/gamemode_sandbox.lua:77: attempt to call method 'Clip1' (a nil value)
1. fn - addons/shapedhud/lua/gamemodes/gamemode_sandbox.lua:77
2. unknown - addons/ulib-v2_63/lua/ulib/shared/hook.lua:109
And posting the whole code wouldnt help, because everything thats about ammo and weaponname is the code i already shown. Everything else is armor, healthbar etc. stuff that has nothing to do with it
Can you show the UPDATED block of code you had before?
This is the Code block with the IsValid
if (Player:GetActiveWeapon():IsValid()) then
local Clip1Ammo = Player:GetActiveWeapon():Clip1()
local WeaponName = Player:GetActiveWeapon():GetPrintName()
draw.RoundedBox(0,ScrW - 112,ScrH - 82, 100 + 4 ,30 + 4,Color(40,40,40))
draw.SimpleText(Clip1Ammo,"HUD", ScrW - 62,ScrH - 67, Color(255,255,255),1,1)
draw.SimpleText("","HUD", ScrW - 62,ScrH - 67, Color(255,255,255),1,1)
if Clip1Ammo <= 0 then
draw.RoundedBox(0,ScrW - 112,ScrH - 82, 100 + 4 ,30 + 4,Color(40,40,40))
draw.SimpleText("No Ammo","HUD", ScrW - 62,ScrH - 67, Color(255,255,255),1,1)
end
draw.RoundedBox(0,ScrW - 176,ScrH - 42, 168 ,30 + 4,Color(40,40,40))
draw.SimpleText(WeaponName ,"HUD", ScrW - 93,ScrH - 25, Color(255,255,255),1,1)
end
Try deleting your Lua cache or resaving the file - that code is correct and doesn't give an error for me.
And my cache would be deleted how? I maybe could show you the whole code, but then not in puplic forums..the code is my whole hud
Delete your ~/garrysmod/cache/ folder.
Ok now i dont get a script error anymore, but the icons of weapon name or ammodisplay dissapear. Oh wait thats because they are in the IsValid check right?
Yes, it won't appear when they're dead
Alright now my HUD is Lua Error free. THX
Sorry, you need to Log In to post a reply to this thread.