Bad Argument #1 with DrawText??

So i’m randomly getting this. I never go it before and now



[ERROR] gamemodes/casino/gamemode/cl_init.lua:34: bad argument #1 to 'DrawText' (string expected, got nil)
  1. DrawText - [C]:-1
   2. unknown - gamemodes/casino/gamemode/cl_init.lua:34



CL.INI.lua


include( 'shared.lua' )

function HUDHide( HUD )
	for k, v in pairs{"CHudHealth","CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"} do
		if HUD == v then 
			return false end
	end
end
hook.Add("HUDShouldDraw","HudHide", HUDHide)

function GM:HUDPaint() --Paint our HUD

 self.BaseClass:HUDPaint()
 
 local ply = LocalPlayer()
 
 local HP = ply:Health()
 local Ammo = ply:GetActiveWeapon():Clip1()
 local TotalAmmo = ply:GetAmmoCount(ply:GetActiveWeapon():GetPrimaryAmmoType())
 end
 
 
surface.CreateFont("CASINO HUD", { --s1 becomes the custom name of font
size = 20, --Size
weight = 900, --Boldness
antialias = true,
shadow = false,
font = "Arial"}) -- Base Font

surface.SetTextColor(255, 255, 255, 255)
surface.SetTextPos(32, 32)
surface.SetFont( "CASINO HUD" )

surface.DrawText( Ammo )
surface.SetTextPos(1390, 825)
surface.DrawText( TotalAmmo )
surface.SetTextPos(1390, 825)

Edit i manage to fix it when messing around with the code but when i restart it happends again

you have no clue what you are trying to do

learn lua

thats because you are trying to show ammo, on a weapon with no ammo.
add this line before: surface.DrawText( Ammo )
[LUA]
if (Ammo == nil or TotalAmmo == nil) then return end
[/LUA]

That fixed the error but the HUD does not show up

Edit: Also when i die or have no weapon how do i fix this




[ERROR] gamemodes/casino/gamemode/cl_init.lua:18: attempt to call method 'Clip1' (a nil value)
  1. unknown - gamemodes/casino/gamemode/cl_init.lua:18



  1. You aren’t drawing it in hudpaint
  2. You are not drawing anything because the variables are nil and you return if they are nil
  3. [del]you are “caching” the totalammo and ammo and not updating them every frame[/del] actually those vars are out of scope
  4. You are not checking if localplayer is valid nor the weapon

If i draw it in HUDPaint it crashes. When i check the game it uses RAM up to 2gb

I appreciate the list but i would like is you tell me how to fix them.

[lua]
include( ‘shared.lua’ )
local pHide = {
CHudHealth = true;
CHudBattery = true;
CHudAmmo = true;
CHudSecondaryAmmo = true;
};

function GM:HUDHide( HUD ) – use gamemode function since we create our own gamemode
return pHide[HUD]; – faster
end

surface.CreateFont(“CASINO HUD”, { --s1 becomes the custom name of font
size = 20, --Size
weight = 900, --Boldness
antialias = true,
shadow = false,
font = “Arial”
}) – Base Font
function GM:HUDPaint() --Paint our HUD – good tabbing (no more c+p effects)
self.BaseClass:HUDPaint()

local ply = LocalPlayer()
if(not IsValid(ply)) then return; end

local HP = ply:Health()
local Ammo = 0;
local TotalAmmo = 0;
if(IsValid(ply:GetActiveWeapon())) then
	Ammo = ply:GetActiveWeapon():Clip1()
	TotalAmmo = ply:GetAmmoCount(ply:GetActiveWeapon():GetPrimaryAmmoType())
end

surface.SetTextColor(255, 255, 255, 255)
surface.SetTextPos(32, 32)
surface.SetFont( "CASINO HUD" )

surface.DrawText( Ammo )
surface.SetTextPos(1390, 825)
surface.DrawText( TotalAmmo )
surface.SetTextPos(1390, 825)

end
[/lua]

i felt like being nice

also you might want to change the SetTextPos areas to make it work with all screen resolutions…

Thank you so much

Also no prob

check my extra edit on that post