The table you’ve defined there is the equivalent of doing:
[lua]
local hl2wep = { }
hl2wep[ nil ] = 17
hl2wep[ nil ] = 6
hl2wep[ nil ] = 45
hl2wep[ nil ] = 30
hl2wep[ nil ] = 6 – etc
[/lua]
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.
[lua]
local pl
hook.Add( “InitPostEntity”, “”, function( ) pl = LocalPlayer( ) end )
local function Example( )
if ( !pl ) then return end
print( pl:GetPos( ) )
end
[/lua]
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
[lua]
if ( weapon_obj && weapon_obj:IsValid( ) ) then
[/lua]
/critic