Clip Size?

What’s the best way of making the clip size for HUDs?

You can put each weapons clip size in a table, but that means everything has to be defined for it to work
You can make a variable which changes between each weapon that is pushed up when the current ammo > clip size, but then it fails with weapons that push their ammo up above the clip amount

Or am I just completely missing a function? :downs:

If the weapon uses the CS base you can do: Weapon:GetTable().Primary.ClipSize

Doesn’t work on the HalfLife weapons though as far as I remember. For that I just did as you said and stored the max clip size as a variable. Then I got the active weapon and checked the class name against the pre-stored value.

If you want the current number of bullets in the primary clip use: Weapon:Clip1()

oh well I’ll try it anyway

EDIT: God damnit, no it doesn’t.

I think I’m done here.

Just make a table for HL2 weapons.

local hl2_weapons =
[ “weapon_pistol” ] = 18,
[ “weapon_357” ] = 6,
[ “weapon_physgun” ] = -1, --etc etc

– Later on in the script for checking the clipsize

local clip

– weapon being the entity and GetClass referring to it’s classname
local class = weapon:GetClass( )

if ( hl2_weapons[ class ] ) then
– if the classname is on the table then the weapon is writen in C++
clip = hl2_weapons[ class ]
– Otherwise we can get it’s clipsize like this
clip = weapon:GetTable( ).Primary.Clipsize

– or shorter:
local clip = hl2_weapons[ weapon:GetClass( ) ] || weapon:GetTable( ).Primary.Clipsize

– Do whatever with the clip

[lua]local hl2wep = {}
hl2wep[“weapon_pistol”] = 17
hl2wep[“weapon_357”] = 6
hl2wep[“weapon_smg1”] = 45
hl2wep[“weapon_ar2”] = 30
hl2wep[“weapon_shotgun”] = 6
hl2wep[“weapon_grenade”] = sRound
hl2wep[“weapon_rpg”] = sRound
hl2wep[“weapon_slam”] = sRound
hl2wep[“weapon_crossbow”] = sRound

function AAHUD:primaryClipSize()
if !(pl:GetActiveWeapon():GetTable().Primary == nil) then --if it exists
return pl:GetActiveWeapon():GetTable().Primary.ClipSize --then use it
elseif !(hl2wep[pl:GetActiveWeapon()] == nil or sRound) then --if it has a clip, and is a hl2 weapon
return hl2wep[pl:GetActiveWeapon():GetClass()] --then grab it’s value
elseif !(hl2wep[pl:GetActiveWeapon()] == nil) then --if it doesn’t have a clip, and is a hl2 weapon
return pl:GetAmmoCount(pl:GetActiveWeapon():GetPrimaryAmmoType()) --then grab how much ammo is there
else return “N/A” --ergo what are you doing (physgun)

TBH I like making functions more.

You’re doing it wrong.

It works though, :v:

if you’re talking about the weapon -> keys, I’m using hacky shit because I’m lazy; I’ll clean it up in a sec
if you’re talking about my absence of locals, I don’t give a damn

The table you’ve defined there is the equivalent of doing:
local hl2wep = { }

hl2wep[ nil ] = 17
hl2wep[ nil ] = 6
hl2wep[ nil ] = 45
hl2wep[ nil ] = 30
hl2wep[ nil ] = 6 – etc
Which I’m positive isn’t what you want.

In addition, you’re calling GetActiveWeapon( ) six times every time you call that function, which mean if its being called in a HUDPaint hook, you’re calling it roughly 360 times a second. Locally defining it once at the beginning of the function and using that variable instead of calling over and over is what you should be doing.

I’m going to assume you have AAHUD defined but pl isn’t, unless you’ve defined it somewhere else as the local player. However, if you do have it defined, you’re not going to be able to use your script in autorun because you’ll be calling LocalPlayer( ) before there is even worldspawn which will return a null entity. The solution is to define it when the InitPostEntity hook gets called so you know that the world exists and then checking if it’s valid each time before use.
local pl
hook.Add( “InitPostEntity”, “”, function( ) pl = LocalPlayer( ) end )

local function Example( )
if ( !pl ) then return end

print( pl:GetPos( ) )

Not checking if the player’s active weapon is valid and calling an entity meta function on it will give you errors in many cases. Check
if ( weapon_obj && weapon_obj:IsValid( ) ) then


I know you’re trying to help, and I appreciate it.