Sound keeps repeating at empty clip.


function SWEP:Think()
self:Stuff()
	if CLIENT then return end
	if ( self.Owner:KeyPressed( IN_ATTACK ) ) then
		if self.Owner.lastpressed == nil or self.Owner.lastpressed + 0.5 < CurTime() then
		self.Owner.lastpressed = CurTime()
		self:PrimaryAttack()
			if (SERVER) then if !( ( self.Weapon:Clip1() ) > ( self.Primary.ClipSize ) ) then self.Weapon:EmitSound("v_hmgk/hmg.wav") end end -- this one

		self.doubleshoot = CurTime() +.1
		end
	end
	
	if self.doubleshoot and CurTime() > self.doubleshoot then
		self.doubleshoot = nil
		self:PrimaryAttack()
		self.tripleshoot = CurTime() +.1
	end
	
	if self.tripleshoot and CurTime() > self.tripleshoot then

		self.tripleshoot = nil
		self.doubleshoot = nil
		self:PrimaryAttack()
	end
	end


I’ve tried replacing the line I commented with this one:


if (SERVER) and !( ( self.Weapon:Clip1() ) > ( self.Primary.ClipSize ) ) then self.Weapon:EmitSound("v_hmgk/hmg.wav") end

But this ruined the function and the weapon began firing the different way.
And another rather dumb question, how to disable reload sound looping when clip and ammo are empty?

Bump.

Are you getting any errors or is it just not working the way you want it to? Also, wouldn’t it be better to put this in SWEP:PrimaryAttack() rather than Think? I just don’t see why you need to put it there as it’s run every tick which in this case doesn’t need to be done?

It just plays the sound when the clip is empty, though it’s not supposed to.

Look at how it’s done in the default Garry’s Mod weapon base. Perhaps you could add these two functions to your weapon code and modify them to your liking?

[lua]
–[[---------------------------------------------------------
Name: SWEP:CanPrimaryAttack( )
Desc: Helper function for checking for no ammo
-----------------------------------------------------------]]
function SWEP:CanPrimaryAttack()

if ( self.Weapon:Clip1() &lt;= 0 ) then

	self:EmitSound( "Weapon_Pistol.Empty" )
	self:SetNextPrimaryFire( CurTime() + 0.2 )
	self:Reload()
	return false
	
end

return true

end

–[[---------------------------------------------------------
Name: SWEP:CanSecondaryAttack( )
Desc: Helper function for checking for no ammo
-----------------------------------------------------------]]
function SWEP:CanSecondaryAttack()

if ( self.Weapon:Clip2() &lt;= 0 ) then

	self.Weapon:EmitSound( "Weapon_Pistol.Empty" )
	self.Weapon:SetNextSecondaryFire( CurTime() + 0.2 )
	return false
	
end

return true

end
[/lua]

I use my own base.

You can still use those functions though, right?

Indeed, rate me dumb for my stupidity please.
Oh yeah, the sound is in Think function, and I already have SWEP:CanPrimaryAttack like this in my own base.

You should attempt to minimize use of Think and Tick hooks and functions as much as possible. Use the PrimaryAttack function instead of using a think hook and checking for IN_ATTACK. A single think hook may not seem like much but if you add all them together it poses as a serious slow-down to server efficiency, especially in a single-threaded environment.

On console type stopsound if it keeps repeating.

Are you still trying to sort out this issue? If so, let me clarify what you want to do exactly. Do you just want to disable the empty clip sound orr? I also agree with what OzymandiasJ said.

it’s not a clip it’s a magazine.

Yes but it’s referred to as clip in Garry’s Mod lua.