SWEP's Random Sounds on Multiplayer Playing Twice

Hello Facepunch. :smile:
I am developing a small SWEP, and I need a bit of help.
Currently when the user attacks using the SWEP, it will play two different sounds, each from its own selection.

-The sound emitted by the weapon itself is a random swing sound. (This sound is only intended to be heard by the weapon user)
-The sound emitted by the weapon owner is a taunt sound. (This sound is intended to be heard by everybody nearby)

In singleplayer this works no problem; you hear 1 of 2 different swing sounds, and 1 of 6 different taunt sounds.
But in multiplayer, the sound emitted by the owner of the weapon (in this case, the taunt sound) is emitted twice.
And it isn’t even the same sound that is emitted twice, it is 2 different sounds from that selection.
[lua]function SWEP:PrimaryAttack()
self.Weapon:EmitSound(“slash_0”…math.random(1, 2)…".wav") --Works fine in both singleplayer and multiplayer
local attack = math.random(1, 6)
self.Owner:EmitSound(“attack_0”…attack…".wav") --Plays two random sounds in multiplayer, but works as intended in singleplayer
self:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
end[/lua]
I think I have an idea as to why this happens but I can’t come up with a solution. Any ideas would be appreciated.
(If I choose to emit both of the sounds from the weapon, they won’t be heard in multiplayer (I believe, I may be mistaken) and they will cut eachother off)

From the wiki:

I think that’s why you’re getting repeating sounds

Thanks for the quick response. I tried playing around with that but I couldn’t get much to change, could somebody help me out?

What are you trying?

[lua]function SWEP:PrimaryAttack()
self.Weapon:EmitSound(“slash_0”…math.random(1, 2)…".wav") --Works fine in both singleplayer and multiplayer
local attack = math.random(1, 6)
if not IsFirstTimePredicted() then return end
self.Owner:EmitSound(“attack_0”…attack…".wav") --Plays two random sounds in multiplayer, but works as intended in singleplayer
self:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
end[/lua]
I understand I am probably not using it properly, but I am faily new to lua.

Bump.

Anyone have any ideas? :ohno:

this is probably due to you having non-predicted random numbers generated.
try using a shared random function to fix that.

I don’t believe it has so much to do with the random numbers as it does the PrimaryAttack function.
For example, when I make the PrimaryAttack function print “Test” to console (as seen below):
[lua]function SWEP:PrimaryAttack()
self.Weapon:EmitSound(“slash_0”…math.random(1, 2)…".wav") --Works fine in both singleplayer and multiplayer
local attack = math.random(1, 6)
self.Owner:EmitSound(“attack_0”…attack…".wav") --Plays two random sounds in multiplayer, but works as intended in singleplayer
self:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
print(“Test”) <-------
end[/lua]
It prints “Test” once to console in singleplayer, and twice to console in multiplayer.
If I have it play a single specific sound (one that isn’t random), in multiplayer, that sound will be played twice, making out louder and sound bad. :excited:

it is the random being different on client and server states, and in conjunction with you not doing self:EmitSound, instead of doing self.Owner:EmitSound

since you are emitting sound on the player it won’t be predicted properly afaik

I appreciate your thought but that stiill doesn’t answer why it would print a test message to console twice per single primary fire.

I believe it has something to do with what MPan1 said earlier, but if he’s right, I’ll need some help getting it to work.

http://wiki.garrysmod.com/page/Prediction offers loads of help.