Finding max mag. size

I’m making a HUD, and I need to figure out the max amount of ammunition in a magazine to get some scaling right. I’m not creating this for some gamemode so the dynamic acquisition of the max mag. size is pretty important.

LocalPlayer():GetActiveWeapon():GetTable().Primary.ClipSize works well with SWeps, but considering the regular HL2 weapons have nothing but resetbones in their tables it’s not a viable solution if I actually want this thing to work.

Here’s a little snippet from the code, with variable assignments added to avoid any confusion:

local Health = LocalPlayer():Health()
local wep = LocalPlayer():GetActiveWeapon()
local RemainingAmmo = wep:Clip1()
local MaxClipSize = 0

if Health > 0 then
	if wep:GetTable().Primary == nil then
		if RemainingAmmo > MaxClipSize then
			MaxClipSize = RemainingAmmo
		MaxClipSize = wep:GetTable().Primary.ClipSize
	MaxClipSize = 0

As you can see I tried to use if RemainingAmmo > MaxClipSize then MaxClipSize = RemainingAmmo end but for some reason Gmod decides to bring the value of MaxClipSize down with RemainingAmmo whenever any ammunition’s spent.


Get out a fully loaded SMG, MaxClipSize will be set to 45. Shoot a bullet and both the RemainingAmmo value and MaxClipSize value decreases to 44.

Is there any reason as to why this happens? Is there any more efficient and dynamic way to get the max clip size (from HL2 guns specifically)?

The only answer I’ve seen posted is: there’s only like a dozen weapons, just hardcode it for them.
In fact, I think someone may have posted the information, I’ll see if I can find it.

EDIT: Nope, couldn’t find it. Just a few threads that led me back to hardcoding it

As said, HL2 weapons have their stuff in C which is not accessible in lua. Simply hardcode the amounts, join sandbox and write them down.

Alright, I’ll do that, then. Thanks a bunch.