Limiting ammo on a SWEP

Hey guys; I am trying to implement this sandbox SWEP C4 into a gameserver, and right now it works absolutely fine except that players have infinite ammo. How would I go about

  1. Setting an ammo cap
  2. Making sure that players cannot throw out another c4 if they dont have the ammo for it.
    If anyone is wondering, the c4 works by right clicking to throw out c4 and left clicking to detonate all c4 entities (in a chain)
    [lua]AddCSLuaFile( “shared.lua” )

SWEP.Author = “Hoff”
SWEP.Instructions = “”

SWEP.Category = “CoD Multiplayer”
SWEP.Spawnable = true
SWEP.AdminSpawnable = true

SWEP.ViewModel = “models/hoff/weapons/seal6_c4/v_c4.mdl”
SWEP.WorldModel = “models/hoff/weapons/seal6_c4/w_c4_0-5.mdl”
SWEP.ViewModelFOV = 70

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = “slam”
SWEP.Primary.Delay = 1

SWEP.Secondary.ClipSize = 4
SWEP.Secondary.DefaultClip = 4
SWEP.Secondary.Automatic = true
SWEP.Secondary.Ammo = “none”

SWEP.Weight = 5
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false

SWEP.PrintName = “C4”
SWEP.Slot = 4
SWEP.SlotPos = 1
SWEP.DrawAmmo = false
SWEP.DrawCrosshair = false

hook.Add(“Initialize”,“createc4convar”,function()
if ConVarExists( “c4_infinite” ) == false then
CreateConVar(“c4_infinite”, 1)
end
end)

SWEP.Offset = {
Pos = {
Up = 0,
Right = -2,
Forward = 0,
},
Ang = {
Up = 0,
Right = 0,
Forward = -45,
}
}

function SWEP:DrawWorldModel( )
local hand, offset, rotate

if not IsValid( self.Owner ) then
self:DrawModel( )
return
end

if not self.Hand then
self.Hand = self.Owner:LookupAttachment( “anim_attachment_rh” )
end

hand = self.Owner:GetAttachment( self.Hand )

if not hand then
self:DrawModel( )
return
end

offset = hand.Ang:Right( ) * self.Offset.Pos.Right + hand.Ang:Forward( ) * self.Offset.Pos.Forward + hand.Ang:Up( ) * self.Offset.Pos.Up

hand.Ang:RotateAroundAxis( hand.Ang:Right( ), self.Offset.Ang.Right )
hand.Ang:RotateAroundAxis( hand.Ang:Forward( ), self.Offset.Ang.Forward )
hand.Ang:RotateAroundAxis( hand.Ang:Up( ), self.Offset.Ang.Up )

self:SetRenderOrigin( hand.Pos + offset )
self:SetRenderAngles( hand.Ang )

self:DrawModel( )
end

function SWEP:Deploy()
self.Owner.C4s = self.Owner.C4s or {}
–self:SetModelScale( 0.35, 0 )
end

local reg = debug.getregistry()
local GetVelocity = reg.Entity.GetVelocity
local Length = reg.Vector.Length
local GetAimVector = reg.Player.GetAimVector
local inRun = false
function SWEP:Think()
vel = Length(GetVelocity(self.Owner))
if self.Owner:OnGround() then
if !inRun and vel > 2.4 and self.Owner:KeyDown(IN_SPEED) then
self.Weapon:SendWeaponAnim(ACT_VM_PULLPIN)
inRun = true
elseif( inRun and !self.Owner:KeyDown(IN_SPEED) ) then
self:SendWeaponAnim(ACT_VM_IDLE)
inRun = false
end
end
end

function SWEP:Equip(NewOwner)

if GetConVar(“c4_infinite”) == 0 then
self.Owner:GiveAmmo(5,“slam”)
end
end

function SWEP:PrimaryAttack()
self:SendWeaponAnim(ACT_VM_PRIMARYATTACK)

timer.Simple(0.1,function() if IsValid(self) then self:EmitSound(“hoff/mpl/seal_c4/c4_click.wav”) end end)
if self.Owner:Alive() and self.Owner:IsValid() then
– thanks chief tiger
local Owner = self.Owner
if SERVER then
for k, v in pairs( Owner.C4s ) do
timer.Simple( .05 * k, function()
if IsValid( v ) then

			v:Explode()
		end				
		table.remove( Owner.C4s, k )
	end )
end

end
end

self:SetNextPrimaryFire(CurTime() + 1.1)
self:SetNextSecondaryFire(CurTime() + 1.2)
end

function SWEP:SecondaryAttack()
if GetConVar(“c4_infinite”) == 0 and GetConVar(“c4_infinite”) ~= nil then
if ( self:Ammo1() <= 0 ) then return false end
self:TakePrimaryAmmo(1)
end
self:SendWeaponAnim(ACT_VM_THROW)
self:EmitSound(“hoff/mpl/seal_c4/whoosh_00.wav”)
–self:TakePrimaryAmmo(1)

local tr = self.Owner:GetEyeTrace()

if SERVER then
local ent = ents.Create(“nz_cod-c4”)

ent:SetPos(self.Owner:GetShootPos())
ent:SetAngles(Angle(1,0,0))
ent:Spawn()
ent.C4Owner = self.Owner		

local phys = ent:GetPhysicsObject()
phys:SetMass(0.6)
local shot_length = tr.HitPos:Length()
local aimvector = self.Owner:GetAimVector()

– phys:ApplyForceCenter (self.Owner:GetAimVector():GetNormalized() * math.pow (shot_length, 3));
– phys:ApplyForceCenter(aimvector*500000)
phys:ApplyForceCenter( self.Owner:GetAimVector()1500)
local angvel = Vector(0,math.random(-5000,-2000),math.random(-100,-900)) //The positive z coordinate emulates the spin from a right-handed overhand throw
angvel:Rotate(-1
ent:EyeAngles())
angvel:Rotate(Angle(0,self.Owner:EyeAngles().y,0))
phys:AddAngleVelocity(angvel)
–ent:SetVelocity(ent:GetForward()*50000 + ent:GetRight()*50 + ent:GetUp()*50)
ent:SetGravity(50)
table.insert( self.Owner.C4s, ent )
end
self:SetNextPrimaryFire(CurTime() + 1.1)
self:SetNextSecondaryFire(CurTime() + 1.2)
end

function SWEP:ShouldDropOnDie()
return false
end

function SWEP:Reload()
self:SendWeaponAnim(ACT_VM_RELOAD)
end

function SWEP:DrawHUD()
surface.SetDrawColor( 255, 255, 255, 255 )
surface.SetMaterial( Material(“models/hoff/weapons/seal6_c4/c4_reticle.png”) )
surface.DrawTexturedRect( ScrW() / 2 - 16, ScrH() / 2 - 16, 32, 32 )
end[/lua]

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = “slam”
SWEP.Primary.Delay = 1

Make Clipsize and Default clip numbers other than a negative number.

1 already happens as long as the c4_infinite convar is set to 0. It’s set to 1 when created on line 37, so change



CreateConVar("c4_infinite", 1) 


to



CreateConVar("c4_infinite", 0) 


To limit ammo you’d have to use the PlayerCanPickupWeapon hook afaik.