How can I fix this error?

I am making an ammo hud for my gamemode. But, everytime I run it, I get this error:

[lua]
Hook ‘HUDText’ Failed: SomeDM/gamemode/cl_init.lua:23: attempt to call method ‘Clip1’ (a nil value)
[/lua]

Here is the line of code that displays ammo count:

[lua]draw.SimpleText( LocalPlayer():GetActiveWeapon():Clip1(), “ScoreboardText”, ScrW() * 0.93, ScrH() * 0.94, Color( 255, 255, 255, 200 ) )[/lua]

How can I fix this. What is the code to display current ammo?

You need to add a check to see if the weapon is valid.

[lua]local wep = LocalPlayer():GetActiveWeapon()
if wep and wep:IsValid() then
draw.SimpleText( wep:Clip1(), “ScoreboardText”, ScrW() * 0.93, ScrH() *0.94, Color( 255, 255, 255, 200 ) )
end[/lua]

Thank you. It worked.

That’s perfectly true but I just thought I’d point out that doing


IsValid(wep)

or


ValidEntity(wep)

is exactly the same as doing


wep and wep:IsValid()

though I suppose the full example is better for learning purposes.
http://luabin.foszor.com/code/lua/includes/util.lua#160

Well, instead of wasting a new thread, I will post my new problem here. Everytime I put this code in cl_init:

[lua]function pointsHUD()

draw.RoundedBox( 6, 0, ScrH() * 0.785, ScrW() / 10, ScrH() / 25, Color( 80, 80, 80, 255 ) )

end
hook.Add( “HUDPaint”, “pointsHUD”, pointsHUD )[/lua]

All the text in my HUD is gone. But when I remove the code, all text appears again. So I figure it is in this code. Can someone help me?