Weapon doesen't reload or continue firing sounds

What I want to achieve
I want the weapon to continue playing a sound even after I clicked to fire again, the weapon to reload on pressing reload when it has an empty clip.

Code

local ShootSound = Sound(“Pew.mp3”) --Sound it makes when firing.
SWEP.Primary.TakeAmmo = 1 – How much ammo will be taken per shot.
SWEP.Primary.ClipSize = 7 – How much bullets are in the mag.
SWEP.Primary.Ammo = “Pistol” --The ammo type will it use.
SWEP.Primary.DefaultClip = 42 – How much bullets preloaded when spawned.
SWEP.Primary.NumberofShots = 1 – Number of bullets when shot.
SWEP.Primary.MaxAmmo = 35

SWEP.DrawAmmo = true

SWEP.FiresUnderwater = true

SWEP.CSMuzzleFlashes = true

function SWEP:PrimaryAttack()

if ( !self:CanPrimaryAttack() ) then return end

local bullet = {}
bullet.Num = self.Primary.NumberofShots
bullet.Src = self.Owner:GetShootPos()
bullet.Dir = self.Owner:GetAimVector()
bullet.Spread = Vector( self.Primary.Spread * 0.1 , self.Primary.Spread * 0.1, 0)
bullet.Tracer = 1
bullet.Force = self.Primary.Force
bullet.Damage = self.Primary.Damage
bullet.AmmoType = self.Primary.Ammo

local rnda = self.Primary.Recoil * -1
local rndb = self.Primary.Recoil * math.random(-1, 1)

self:ShootEffects()

self.Owner:FireBullets( bullet )
self:EmitSound(ShootSound)
self.Owner:ViewPunch( Angle( rnda,rndb,rnda ) )
self:TakePrimaryAmmo(self.Primary.TakeAmmo)

self:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
end

function SWEP:Reload()
self:EmitSound(Sound(self.ReloadSound))
self.Weapon:DefaultReload( ACT_VM_RELOAD );
end

What I’ve researched/found

For the ammo issue, I found out its because the SWEP.Primary.DefaultClip being 1, I’ve changed it up, but It still didn’t work.

For the audio issue, I tried adding in a sound table, a sound.Add function, but nothing works from what I’ve experimented.

Let’s get this straight, what you need is the following:

  1. After firing a bullet, you want a second sound to play after some time
    For this you can use timer.Simple - Garry's Mod Wiki, so something like this would work:
    timer.Simple(0.5, function() self:EmitSound(ShootSound) end)

  2. The weapon to replenish ammo after hitting the Reload button
    The code looks good for what you are trying to do, I do not understand what the problem is here. What does “pressing reload when it has an empty clip” mean? Do you want it so you can only reload it when the clip is empty or does the gun not reload at all in its current state?

1 Like

Thank you VERY much for help!

What I mean by the sound issue is, the sound gets “cut-off”, it doesen’t play fully if i fire while the last one is coming to an end. (Sound is custom)

The reloading issue, my gun has unlimited ammo EVEN IF THE CLIP HAS 0 BULLETS!, it doesent need to reload, i want it so that when the clip reaches 0 bullets, the player can only press reload, to well, reload. (Maybe cause I didn’t code in any reload sound?, It plays a stock one)

Add this code inside a SWEP:Initialize() function:

sound.Add({
name = "MyWeapon_PrimaryFire",
channel = CHAN_WEAPON,
volume = 1.0,
level = 75,
pitch = {90, 110}, 
sound = "Pew.mp3"})

And try playing the sound in the PrimaryAttack() function:

self:EmitSound("MyWeapon_PrimaryFire")

As for the reloading issue, you need to use the TakePrimaryAmmo() function BEFORE calling self.Owner:FireBullets(), if you have no ammo it won’t run anything that comes next. (This means you also need to put SetNextPrimaryFire before TakePrimaryAmmo, or else if you have no ammo it will do it every frame)

1 Like

Thanks alot!