GMOD: Primary ammo rising

In SWEP:Think how to I have the primary ammo going up at all times, like in the health pack swep that is default with gmod


function SWEP:Think()
  if SWEP.Bool == false then
    --Ammo goes up, what do I put here guys?
  end
end


SWEP.ChargeStatus = 0;
function SWEP:Think()
	self.ChargeStatus = math.Clamp( self.ChargeStatus + 1, 0, 100 ); // Clamp value min max
end

Although, that’ll go up REALLY fast. I’d recommend doing things based on TIME.


SWEP.ChargeStatus = 0;
SWEP.LastCharge = 0;
SWEP.ChargeRatePerSecond = 1;
function SWEP:Think()
	local _dt = 0;
	if ( self.LastCharge > 0 ) then
		_dt = CurTime( ) - self.LastCharge( );
		self.LastCharge = CurTime( );
	end
	self.LastCharge = CurTime( );

	self.ChargeStatus = math.Clamp( self.ChargeStatus + ( SWEP.ChargeRatePerSecond * _dt ), 0, 100 ); // Clamp value min max
end

Thank you, how would the swep know to display chargeStatus rather than PrimaryAmmo?

Just have the SWEP:DrawHUD( ) show that instead. You could also have the variable be the primary ammo, I used charge as an example.

Check the post, I updated it regarding the charge-rate.

What is the variable for primary ammo?

If you just want to display it, you can use

self.Owner:GetAmmoCount( self.Primary.Ammo ); - You may be able to use Ammo1( ), Ammo2( ), Clip1( ), Clip2( ), etc for getting.

If you want to set the ammo, take a look at the reload function, or give player ammo - only do it serverside and it should network. If you incorporate a new variable, you’d need to network it manually.

Okay I found a good reload example on maurits.
I will post the answer later for future readers.

Thank you very much!