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]
[QUOTE=WhiTAkeR;21446794]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][/QUOTE]
Thank you. It worked.
[QUOTE=WhiTAkeR;21446794]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][/QUOTE]
That's perfectly true but I just thought I'd point out that doing
[code]IsValid(wep)[/code]
or
[code]ValidEntity(wep)[/code]
is exactly the same as doing
[code]wep and wep:IsValid()[/code]
though I suppose the full example is better for learning purposes.
[url]http://luabin.foszor.com/code/lua/includes/util.lua#160[/url]
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?
Sorry, you need to Log In to post a reply to this thread.