Getting a SWEP's Primary.Sound to use a table instead of using Emit.Sound in a function?

Hi FPers!

As opposed to using the method of having Emit.Sound inside the PrimaryAttack function,
is it possible to assign a SWEP’s “SWEP.Primary.Sound = Sound ()” to efficiently use a table of sounds randomly?

The reason I’d like it set up this way is because I am using Spectator Deathmatch for TTT and although the weapon sounds from dead players are muted through the coding for alive players, emit.sounds are not. I guess another solution would be to have the Emit.Sounds muted too, but… ehh.

Any ideas?

Here’s my current code using Emit.Sounds:


local randomsounds = {
"weapons/fingergun/pew1.wav",
"weapons/fingergun/pew2.wav",
"weapons/fingergun/pew3.wav",
"weapons/fingergun/pew4.wav"
}

function SWEP:PrimaryAttack()
		
local randomSound = table.Random(randomsounds)
self.Weapon:EmitSound( "randomSound")

etc weapon code... etc..
   

Make sure the sounds are registered using sound.Add on the WEAPON_CHANNEL with a name like XWeapon.Single, etc…

Just emitting the .wav, you’ll end up with them not always playing, etc…

Thank you for the fast response Acecool!
This is what I’ve got from sound.Add, would I just throw this into the .lua for the weapon?


sound.Add({
	name = "pew1",
	channel = CHAN_WEAPON,
	volume = 1.0,
	soundlevel = 145,
	pitchstart = 100,
	pitchend = 100,
	sound = "weapons/fingergun/pew1.wav"
});

for each sound

You’d use self.Weapon:EmitSound( “pew1” );

Actually, self.Weapon is “deprecated” last I heard; self:EmitSound( “pew1” ); would work.

If you name them 1-5 you can do a less expensive call using:

[lua]self.EmitSound( “pew” … math.random( 1, 5 ) );[/lua]

Remember, Wrap it in if SERVER then to prevent different sounds playing; or at least use IsFirstTimePredicted to prevent the client from calling it many times.