This isn't really a good tutorial, but oh well.
If you want random sounds on a npc, you may want to use a table with the sound ID's in it, and then select a random one from it, and insert it into a sound.
Incase you dont know what a table is, this is one.
local table = { var1, var2, var3 }
As for math.random() you'll want to use something that automatically detects how many things are in a table, just because you dont need to change the math.random() after adding more sounds that way.
local var4 = math.random( 1, 10 )
This will pick a random number from 1 to 10 that var4 will become.
To use the automatical table content amount detection as stated before, it's really simple!
Just switch out the 10 in math.random( 1, 10 ) with #table.
Doing so will make the math.random() go from 1 to however many things there are in the table.
local soundtable = { sound1, sound2, sound3, sound4 }
local ran = math.random(1,#soundtable)
sound.SoundId = soundtable[ran]
In the code stated above, soundtable[ran] checks the table "soundtable" for any content at whatever number "ran" is.
Alternatively, to shrink the code, you can use the following
local soundtable = { sound1, sound2, sound3, sound4 }
sound.SoundId = soundtable[math.random( 1, #soundtable )]
Hopefully this helped in some way :|
Also, you can use math.random in a variety of different ways, the way i just showed is just the one I prefer.
Another more inefficient but less advances way, is the following
local var = math.random(1,3)
if var == 1 then
sound.SoundId = "id1"
elseif var == 2 then
sound.SoundId = "id2"
elseif var == 3 then
sound.SoundId = "id3"
end
im bad at doing tutorials allright?
2 comments
why -1??
What exactly is your problem with playing the sounds?