SWEP - Changes sound

Hello, i’m trying to make a SWEP that plays a sound when you left click and changes the sound when you right click. So far i have this


 
function SWEP:PrimaryAttack()

	sound = 1

	if ( sound == 1 ) then
		self:EmitSound("physics/body/body_medium_break2.wav")
	end

	if ( sound == 2 ) then
		self:EmitSound("physics/cardboard/cardboard_box_break1.wav")
	end

end

function SWEP:SecondaryAttack()

	sound = sound + 1
	if sound > 2 then sound = 1 end

end


It doesn’t seem to be working, can anybody help? Thanks.

You’re emitting the sound from the weapon. Emit it from self.Owner. Also, use a SWEP variable for sond and not a global value.

sound = 1 should be placed outside the function if he wants to keep it global (but really don’t)

Instead use SWEP.SoundNum and access it in functions like self.SoundNum.

This might help use this code might work I use it in my servers.



function SWEP:Think()

	if CLIENT then

		if input.IsKeyDown( KEY_E ) and input.IsKeyDown( KEY_R ) then --NOTE:The sound emits when you press E and R At the same time, you can change this.

			// The IsValid checks if the menu exists so that this won't run every frame you hold
			// both keys
			// Create menu base
			self:EmitSound("PUT SOUND HERE")
			// etc....


		end

	end

end


As code_gs says, do something like SWEP.WeaponSound = “sound_name”;

Don’t call it in think…

As PrimaryAttack and SecondaryAttack is SHARED ( gets spammed on the client if you don’t use IsFirstTimePredicted [ which is true when the client and server are in sync ], and called once on the server ) you can do self.WeaponSound = “othersound”; and leave everything else as is in terms of playing the sound ( but remove your current code, you only need to play the sound once ), firing bullet, etc…

Because the SERVER networks the sound no other clients need to know the sound… Only the server and the owner ( which the owner will know when they shoot ) need to know. The sound is played locally for the owner which is why there is no delay for the sound ( try only emitting the sound from the server and you’ll see what I mean ) but with the networking for all other clients, it appears to be just as consistent…