Ammo HUD

Alright so, I’ve been working on custom huds for something i’m working on, the last one I need to make is the custom ammo hud, and I can’t seem to find any documentation on how it’s done, or anything that is entirely helpful, sofar I threw this together, but how would I actually have the ammo draw on the hud, how would I have it display correctly? Help would be appreciated.



function ammo()
	local client = LocalPlayer()
	
	if !client:GetActiveWeapon():IsValid() then return end
	local mag_current = client:GetActiveWeapon():Clip1() // How much ammunition you have inside the current magazine
	local mag_extra = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) // How much ammunition you have outside the current magazine
	
	
	draw.AAText("Ammo: ", "Deathrun_SmoothBig", ScrW() - 165, ScrH() - 50, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
	draw.AAText(client:GetActiveWeapon():Clip1() .. "", "Deathrun_SmoothBig", ScrW() - 105, ScrH() - 48, Color(255, 255, 255, 255), 0, 0 )
	draw.AAText("/ ", "Deathrun_SmoothBig", ScrW() - 60, ScrH() - 50, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
	draw.AAText(client:GetActiveWeapon():GetPrimaryAmmoType() .. "", "Deathrun_SmoothBig", ScrW() - 60, ScrH() - 48, Color(255, 255, 255, 255), 0, 0 )
end


Alright, so I modified it to this, i’m trying to get it to not show if you don’t have a gun/ammo and only show if you have a weapon/ammo With the line that is commented out, when it isn’t it gives an error saying,

[ERROR] addons/ammo_ui/lua/autorun/ammo.lua:12: attempt to call method ‘Clip1’ (a nil value)

  1. fn - addons/ammo_ui/lua/autorun/ammo.lua:12
  2. unknown - addons/ulib/lua/ulib/shared/hook.lua:183
    I’m a bit of a beginner with this stuff and help/pointing me in the right direction would be greatly appreciated.

EDIT: Changed it up again, It shows numbers now without error, but the problem is it shows -1 if the person doesn’t have a proper weapon out, such as a crowbar.

Try adding some IsValid checks.



if !client:GetActiveWeapon():IsValid() then return end


Something I noticed, is when it does work, it shows the value of the ammo to -1, I was wondering how I would make it not show the entire UI if the player isn’t holding/equipping a weapon.

Not sure if this will work or not but you can try.

[lua]
function ammo()
local client = LocalPlayer()
if !client:GetActiveWeapon():IsValid() then return end
if client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) ~= -1 then
local mag_current = client:GetActiveWeapon():Clip1() // How much ammunition you have inside the current magazine
local mag_extra = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) // How much ammunition you have outside the current magazine
draw.AAText(client:GetActiveWeapon():Clip1() … “”, “Deathrun_SmoothBig”, ScrW() - 99, ScrH() - 30, Color(255, 255, 255, 255), 0, 0 )
draw.AAText("Ammo: ", “Deathrun_SmoothBig”, ScrW() - 200, ScrH() - 50, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end
end
[/lua]



function ammo()
	local client = LocalPlayer()
	
	if !client:GetActiveWeapon():IsValid() then return end
	if client:GetActiveWeapon():Clip1() ~= -1 then 
		local mag_current = client:GetActiveWeapon():Clip1() // How much ammunition you have inside the current magazine
		local mag_extra = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()) // How much ammunition you have outside the current magazine
	
	
	draw.SimpleText("Ammo: ", "default", ScrW() - 165, ScrH() - 50, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
	draw.SimpleText(client:GetActiveWeapon():Clip1() .. "", "default", ScrW() - 105, ScrH() - 48, Color(255, 255, 255, 255), 0, 0 )
	draw.SimpleText("/ ", "default", ScrW() - 60, ScrH() - 50, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
	draw.SimpleText(client:GetActiveWeapon():GetPrimaryAmmoType() .. "", "default", ScrW() - 55, ScrH() - 50, Color(255, 255, 255, 255), 0, 0 )
	end
end
hook.Add("HUDPaint", "ammo", ammo)


This is what it currently is, the ammo hud only shows if you’re actually holding a gun, but the problem I have seem to run into is that the PrimaryAmmoType, lets say I have… 0 ammo, it shows a 4 for me. Help would be appreciated.

[editline]1st June 2014[/editline]

I seemed to have fixed it with changing somethong that has been stupidity on my party, help has been appreciated… thanks :slight_smile: