Trouble Displaying Reserve Ammo

I’m trying to make a simple, debug-style hud for myself for the fun of it really.
I’ve run into an issue when it comes to displaying the reserve ammunition for the current weapon in hand.
Here is the portion of code I’m having trouble with.



// Weapon Info HUD
function WeaponHUD()
local weapon = LocalPlayer():GetActiveWeapon()
draw.DrawText("Name: " .. weapon:GetClass() .. " / " .. weapon:GetPrintName(), "BudgetLabel", 965, 1000, Color(255, 255, 255, 255), 1)
draw.DrawText("Ammo: " .. weapon:Clip1() .. " " .. weapon:Clip2(), "BudgetLabel", 965, 1015, Color(255, 255, 255, 255), 1)
draw.DrawText("Ammo Type: " .. weapon:GetPrimaryAmmoType(), "BudgetLabel", 965, 1030, Color(255, 255, 255, 255), 1)
end
hook.Add("HUDPaint", "WeaponHUD", WeaponHUD)


You see, weapon:Clip2() ‘works’ for the default/HL2 weapons. I say ‘works’ because it displays a -1 which wasn’t a surprise since the wiki page for the function told me such, but the problem is, it not only doesn’t display the number for HL2 weapons, but for weapon addons such as M9K it displays a static number. This number doesn’t change when the ammo changes, and I really don’t even know what the number represents.
Anyway, weapon:Ammo1() works for M9k, FAS2, and CW2.0 all of which are very popular and seen all over the place.

weapon:Ammo1() actually displays the number of reserve ammo and everything is fine and dandy, but if you switch to any default/HL2 weapon it will spam you with the Lua error:



[ULib] external:26: attempt to call method 'Ammo1' (a nil value)
  1. fn - external:26
   2. unknown - lua/ulib/shared/hook.lua:110


While I’m here making this post I’d like to ask for help with another feature that is displayed up there in the code. I have it to display the weapon:GetPrimaryAmmoType(), but that displays the ID number for the ammo types, that are found here: https://wiki.garrysmod.com/page/Default_Ammo_Types

Is there a way I can translate these items into the names of the items from say a table or another line of code that when the Lua receives the AmmoType it can use the corresponding translation from the ID into the set name. I already have a table, for the names I want:



AmmoTable = {
	[1] = "item_ammo_ar2 / AR2",
	[2] = "item_ammo_ar2_altfire / AR2AltFire",
	[3] = "item_ammo_pistol / Pistol",
	[4] = "item_ammo_smg1 / SMG",
	[5] = "item_ammo_357 / 357",
	[6] = "item_ammo_crossbow / Bow",
	[7] = "item_box_buckshot / Shotgun",
	[8] = "item_rpg_round / RPG",
	[9] = "item_ammo_smg1_grenade / SGMAltFire",
	[10] = "weapon_frag / Grenade",
	[11] = "weapon_slam / Slam",
	[12] = "weapon_alyxgun_ammo / Alyxgun",
}


Thanks for any help that you can supply me! I really want this issue figured out, I would like to be able to see the reserve ammo for any weapon not custom ones…

Ammo1 is a weapon_base lua-only function. It’s the equivalent of doing LocalPlayer():GetAmmoCount( weapon:GetPrimaryAmmoType() ). Also, Clip2 is the secondary clip of the weapon, which will return -1 if the weapon doesn’t use the secondary clip.

I don’t know if this will help; but to show the ammo and it’s reserves you could do something such as;




if (client:GetActiveWeapon():Clip1() ~= -1) then
    draw.SimpleText(client:GetActiveWeapon():Clip1() .. "/" .. client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()), "DermaDefault", 260, ScrH() - 50, Color(255, 255, 255, 255), 0, 0)
else
    draw.SimpleText(client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()), "DermaDefault", 260, ScrH() - 50, Color(255, 255, 255, 255), 0, 0)
end

if (client:GetAmmoCount(client:GetActiveWeapon():GetSecondaryAmmoType()) > 0) then
    draw.SimpleText("Secondary: " .. client:GetAmmoCount(client:GetActiveWeapon():GetSecondaryAmmoType()), "DermaDefaultBold", 260, ScrH() - 25, Color(255, 255, 255, 255), 0, 0)
end



This if statement will check if the clients active weapon clip is not = -1 and then display the clip and the ammo count of that weapon or if for example you have a grenade in hand, it will show how many grenades you have.

The code after it shows if it has a secondary - so if you have a AR2 and AR2 orbs loaded it will show how many AR2 orbs are loaded.

Wow, thank you guys both so much! I went for iRyno’s reply even though they both worked since it was a nice dynamic touch and I really appreciate both of your guys’ help!
Any ideas on the table thing I was talking about? Being able to translate the givin ammo id’s into text so a player could know exactly what to look for / spawn?
Thanks again!