ERROR While trying to make a HUD.

Hey, ive been making a HUD for ammo and it works :slight_smile: ( thankfully ) Though in my gamemode you dont have a weapon as soon as you spawn. So using what i know i did this
[lua]
if ply:GetActiveWeapon() > 1 then
function --blah blah blah
end
end
[/lua]
That worked, but only with the ply:Clip1()
I tried doing if for the remaining rounds along with the rounds in the clip and it didnt work and this ERROR came up, here is the full code
[lua]
//Ammo Part ( in clip )
local ply = LocalPlayer()
if ply:GetActiveWeapon() > 1 then
function AmmoHUD()
local ply = LocalPlayer()
local AM = ply:GetActiveWeapon():Clip1()

draw.SimpleText( AM, "MyFont2", ScrW() - 150, ScrH() - 50, Color(255,255,255,255))
draw.SimpleText( "Ammo", "MyFont2", ScrW() - 150, ScrH() - 90, Color(255,255,255,255))

end
end
hook.Add( “HUDPaint”, “AmmoHUD”, AmmoHUD )
//( in all )

if ply:GetActiveWeapon() > 1 then
function Ammo2HUD()
local ply = LocalPlayer()
local AM2 = ply:GetAmmoCount(ply:GetActiveWeapon():GetPrimaryAmmoType())

draw.SimpleText( AM2, "MyFont2", ScrW() - 80, ScrH() - 50, Color(255,255,255,255))
draw.SimpleText( "/", "MyFont2", ScrW() - 100, ScrH() - 50, Color(255,255,255,255))

end
end
hook.Add( “HUDPaint”, “Ammo2HUD”, Ammo2HUD )
[/lua]
This is the ERROR



[ERROR] gamemodes/dontneedtoknowyet/gamemode/cl_hud.lua:23: attempt to index global 'ply' (a nil value)


PROBLEM( so you cant say “whats the problem” ): My code will not allow the Ammo2HUD to create. It worked with the first one but the second one didnt, so should i change the code to something? Or what should i change it to?
THIS CODE
[lua]
if ply:GetActiveWeapon() > 1 then
function --blah blah blah
end
end
[/lua]

You should be doing your if checks in your hook functions, not outside… your code is hideous btw.

Yeah um I refuse to read that can you use indentation

Thats what i mean for it to do, so its hard to read if someone where trying to copy it :stuck_out_tongue: But so what you saying it should look like is
[lua]
function myHUD()
if ply:GetActiveWeapon() > 1 then
draw.SimpleText()
end
end
hook.Add ( “HUDPaint”, “myHUD”, myHUD )
[/lua]

Uh sure. Wtf why are you comparing it to a number?? GetActiveWeapon returns the weapon the player has equiped, not a number…

Haha thought someone would ask. Idk i tried it because when the player spawns they dont have a weapon. Soo its tying to draw the HUD without a weapon which causes errors. So i thought of that hoping it would work and it did for one. So im thinking its saying that if the players weapons > 1 then it will draw it. Now thinking about it. Is there you can make it < and = to?

[editline]31st August 2014[/editline]

Ok i tried that and its still saying that error for this function
[lua]
function Ammo2HUD()
if ply:GetActiveWeapon() > 1 then
local ply = LocalPlayer()
local AM2 = ply:GetAmmoCount(ply:GetActiveWeapon():GetPrimaryAmmoType())

draw.SimpleText( AM2, "MyFont2", ScrW() - 80, ScrH() - 50, Color(255,255,255,255))
draw.SimpleText( "/", "MyFont2", ScrW() - 100, ScrH() - 50, Color(255,255,255,255))

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

Define ply inside the function. Also remove the greater than 1 and just detect if the weapon is valid…

Ok thanks :slight_smile: Thats about that for 1 sec btw it is
[lua]
:IsValid()
–or
:!IsValid()
[/lua]
Seen both alot so idk which one would apply to that :slight_smile:
Thanks again :slight_smile:

Seeming as you were trying to compare a weapon to 1; you’ll probably need this:


if IsValid(ply:GetActiveWeapon()) then print("Greater than 1") else print("Less than, or equal to, one") end

Ok i got it :slight_smile: thanks just did this
[lua]
function Ammo2HUD()
local ply = LocalPlayer()
if ply:GetActiveWeapon():IsValid() then
local AM2 = ply:GetAmmoCount(ply:GetActiveWeapon():GetPrimaryAmmoType())

draw.SimpleText( AM2, "MyFont2", ScrW() - 80, ScrH() - 50, Color(255,255,255,255))
draw.SimpleText( "/", "MyFont2", ScrW() - 100, ScrH() - 50, Color(255,255,255,255))

end
end
hook.Add( “HUDPaint”, “Ammo2HUD”, Ammo2HUD )
[/lua]
If you dare to read the ugly code xD