Hey guys, Nik1080 here with another question!
So, a recently I constructed a basic music "playlist" loop:
01 | local Sound 1 = game.Workspace.Sound 1 |
02 | local Sound 2 = game.Workspace.Sound 2 |
03 | local Sound 3 = game.Workspace.Sound 3 |
04 | local Sound 4 = game.Workspace.Sound 4 |
05 | local Sound 5 = game.Workspace.Sound 5 |
06 | local Sound 6 = game.Workspace.Sound 6 |
07 | local Sound 7 = game.Workspace.Sound 7 |
08 |
09 | Assets = { 346073558 , 183479225 , 167337732 , 175755911 , 199372537 , 247303851 , 342274063 } |
10 |
11 | for _, asset in ipairs (Assets) do |
12 | game:GetService( "ContentProvider" ):Preload( "http://www.roblox.com/asset/?id=" .. asset) |
13 | end |
14 |
15 | while true do |
My only problem is that it gets repetitive, in the way that the order is always the same. (Note that in the end I will most likely have more) My question is, is there a way to randomize the order in which the songs are played while waiting the proper amount of time?
This is based off of my comment
A way to have a "random" song play is to use the math.random function & a table, like the one you were using for the Preload; the math.random function could be used to return a random number, then we use that number to get the position from the table, i.e. position 1 in your table is 346073558, and position 4 is 175755911.
Now, w/ that out of the way, lets set up the code! :D
01 | local SoundInstance = game.Workspace:WaitForChild( 'SoundPlay' ) -- The "WaitForChild" function will wait until a child w/ the exact same name as the argument is existent w/in the parent; variable "SoundInstance" is representing the instance |
02 | local SongIDs = { -- Variable "SongIDs" is holding the table containing the song ids; a table is used to store information/ data, and you can retrieve the data via getting a position from the table |
03 | 346073558 , |
04 | 183479225 , |
05 | 167337732 , |
06 | 175755911 , |
07 | 199372537 , |
08 | 247303851 , |
09 | 342274063 |
10 | } |
11 |
12 | math.randomseed(tick()) -- Lets make math.random even more random >:) "tick" returns the UNIX time, which started in January 1st of 1970, and is still counting the time to this day. |
13 |
14 | while true do |
15 | local RandomSong = SongIDs [ math.random( 1 , #SongIDs) ] -- Variable "RandomSong" will represent the ID taken from the table via getting the position it was from; math.random returns a number between numberA & number B |
16 | SoundInstance.SoundId = RandomSong -- Sets the ID property of the "Sound" instance to the song ID retrieved from the table |
17 | SoundInstance:Play() -- Plays the song |
18 | wait( 120 ) -- Yields the code for a specific amount of time b4 going onto the next line; this will wait 2 minutes, but this can be adjusted & coded to support the other audios |
19 | end -- Ends code |
And, wa-la! When you go in-game or the testing mode, you'll hear a random audio play rather than in-order! :D For it to support all the audio's times, however, it's not impossible, but it's more complicated.
Information/ Facts touched on: 1. Tick 2. WaitForChild 3. Math.Random 4. Math.Randomseed
Hope this helped you! :D