• Random sounds?
    57 replies, posted
Yah, how do you make ex. in a swep, primary fire plays 1 random sound every shoot, and for every new one, the old one stops, how do you make it so random?
I shall assist. [lua] local randomsounds = { Sound('sound path here') Sound('sound path here') "etc" } local rndsdn = math.Random(1 , #randomsounds) local randomSound = (Sounds[rndsdn]) [/lua]
There is a new function: [url=http://wiki.garrysmod.com/wiki/?title=Table.random]table.Random[/url] [lua]local randomsounds = { Sound('sound path here') Sound('sound path here') "etc" } local randomSound = table.Random(randomsounds)[/lua]
Or that, I think both would work though :3
I guess table.Random is faster /bitch
1 ns won't make that much of a difference.
[QUOTE=Overv;14643502]I guess table.Random is faster /bitch[/QUOTE] Wow way to be completely wrong. It does practically the same thing but it loops to cater for tables that don't use numeric indexes or start at 1.
and these plays to all on the server at the same time?
[QUOTE=SteveUK;14643658]Wow way to be completely wrong. It does practically the same thing but it loops to cater for tables that don't use numeric indexes or start at 1.[/QUOTE] It could have been a C++ function and faster in that way, jesus... We're here to help people, not to grow your e-penis by flaming people to death
ok thanks >___> ill come back if i need help
[QUOTE=Overv;14644864]It could have been a C++ function and faster in that way, jesus... We're here to help people, not to grow your e-penis by flaming people to death[/QUOTE] Uhh, it is a Lua function, not a C++ one.
It could have been implemented in C++.
Also, you separate table entries with either a comma(,) or a semicolon(;). It is usually preferred to use a comma, though. [code] local t = { "entry 1", "entry 2", "entry 3" }; [/code] So technically *those who posted code* are wrong.
[QUOTE=Nevec;14645458]Also, you separate table entries with either a comma(,) or a semicolon(;). It is usually preferred to use a comma, though. *code* So technically you are all wrong.[/QUOTE] Why am i wrong :tinfoil:?
[QUOTE=Overv;14645368]It could have been implemented in C++.[/QUOTE] Look at the includes folder first.
this is my code: [lua] function SWEP:SecondaryAttack() local randomsounds = { // line 59 Sound('funniez/catbomb.mp3') Sound('funniez/ravebreak.mp3') Sound('funniez/speach/obeyyourthirst2.wav') Sound('funniez/speach/obeyyourthirstsync.wav') Sound('funniez/cant.wav') Sound('funniez/4.wav') Sound('funniez/8.wav') } local randomSound = table.Random(randomsounds) end[/lua] this is my error: [code] weapons/knife/shared.lua:61: '}' expected (to close '{' at line 59) near 'Sound' [/code] help nao
Argh.. [QUOTE=Nevec;14645458]Also, you separate table entries with either a comma(,) or a semicolon(;). It is usually preferred to use a comma, though. [code] local t = { "entry 1", "entry 2", "entry 3" }; [/code] So technically *those who posted code* are wrong.[/QUOTE]
Forgot to include that :3 Nevec is right.
ok ill try rawr Edit: didn't work, it wont play, but no absolute no error, this is the code atm: [lua] function SWEP:SecondaryAttack() local randomsounds = { Sound('funniez/catbomb.mp3'), Sound('funniez/ravebreak.mp3'), Sound('funniez/speach/obeyyourthirst2.wav'), Sound('funniez/speach/obeyyourthirstsync.wav'), Sound('funniez/cant.wav'), Sound('funniez/4.wav'), Sound('funniez/8.wav'), } [/lua]
Maybe because you aren't actually playing it? [code] self.Owner:EmitSound( soundpath ); [/code]
self.Owner:EmitSound( soundpath ); or self.Owner:EmitSound( 'soundpath' );?
[code] self.Owner:EmitSound( soundpath ); [/code] Would work if "soundpath" is a valid string variable. And.. [code] self.Owner:EmitSound( "soundpath" ); [/code] Would work if you just want to pass a string directly. In your case, the first method would work, because you have the sound files stored in a table and then moved to a string variable. Just remember to give it the right variable name.
i got [lua] function SWEP:SecondaryAttack() local randomsounds = { self.Owner:EmitSound('funniez/catbomb.mp3'), self.Owner:EmitSound('funniez/ravebreak.mp3'), self.Owner:EmitSound('funniez/speach/obeyyourthirst2.wav'), self.Owner:EmitSound('funniez/speach/obeyyourthirstsync.wav'), self.Owner:EmitSound('funniez/cant.wav'), self.Owner:EmitSound('funniez/4.wav'), self.Owner:EmitSound('funniez/8.wav') } local randomSound = table.Random(randomsounds) end [/lua] but i should have: [lua] function SWEP:SecondaryAttack() local randomsounds = { self.Owner:EmitSound(funniez/catbomb.mp3), self.Owner:EmitSound(funniez/ravebreak.mp3), self.Owner:EmitSound(funniez/speach/obeyyourthirst2.wav), self.Owner:EmitSound(funniez/speach/obeyyourthirstsync.wav), self.Owner:EmitSound(funniez/cant.wav), self.Owner:EmitSound(funniez/4.wav), self.Owner:EmitSound(funniez/8.wav) } local randomSound = table.Random(randomsounds) end [/lua] ? cuz i tested the first one of mine and they all played the same time and gave me some fucked error: [code] includes/extensions/table.lua:177: bad argument #2 to 'random' (interval is empty) [/code] whats fucked: 1.that path, dosn't exist 2. none of my files got over 150 lines 3. none absolute non is called table.lua O_o
sorry for duble post, i fixed the old error and got this: [code] weapons/knife/shared.lua:60: attempt to index global 'catbomb' (a nil value) [/code] here is the lua code: [lua] local randomsounds = { self.Owner:EmitSound(funniez/catbomb.mp3), self.Owner:EmitSound(funniez/ravebreak.mp3), self.Owner:EmitSound(funniez/speach/obeyyourthirst2.wav), self.Owner:EmitSound(funniez/speach/obeyyourthirstsync.wav), self.Owner:EmitSound(funniez/cant.wav), self.Owner:EmitSound(funniez/orly.mp3), self.Owner:EmitSound(funniez/yarly.mp3) } local randomSound = table.Random(randomsounds) end [/lua]
[lua] local randomsounds = { "funniez/catbomb.mp3", "funniez/ravebreak.mp3", "funniez/speach/obeyyourthirst2.wav", "funniez/speach/obeyyourthirstsync.wav", "funniez/cant.wav", "funniez/orly.mp3", "funniez/yarly.mp3" } local random = math.random(1, #randomsounds) self.Owner:EmitSound(randomsounds[random]) [/lua] works.
table.Random is still nicer :P
will test in like 1 hour, im busy ATM
[QUOTE=Averice;14655732] works.[/QUOTE] Don't listen to Overv, what you did for selecting a random sound works fine.
[QUOTE=Averice;14655732][lua] local randomsounds = { "funniez/catbomb.mp3", "funniez/ravebreak.mp3", "funniez/speach/obeyyourthirst2.wav", "funniez/speach/obeyyourthirstsync.wav", "funniez/cant.wav", "funniez/orly.mp3", "funniez/yarly.mp3" } local random = math.random(1, #randomsounds) self.Owner:EmitSound(randomsounds[random]) [/lua] works.[/QUOTE] testing this...
[QUOTE=Salads;14659844]Don't listen to Overv, what you did for selecting a random sound works fine.[/QUOTE] Have fun rewriting stuff that's been written already.
Sorry, you need to Log In to post a reply to this thread.