Hey guys, Nik1080 here with another question!
So, a recently I constructed a basic music "playlist" loop:
local Sound1 = game.Workspace.Sound1 local Sound2 = game.Workspace.Sound2 local Sound3 = game.Workspace.Sound3 local Sound4 = game.Workspace.Sound4 local Sound5 = game.Workspace.Sound5 local Sound6 = game.Workspace.Sound6 local Sound7 = game.Workspace.Sound7 Assets = {346073558,183479225,167337732,175755911,199372537,247303851,342274063} for _, asset in ipairs(Assets) do game:GetService("ContentProvider"):Preload("http://www.roblox.com/asset/?id=" .. asset) end while true do Sound1:Play() wait (110) -- Note the all the waits are one second longer than the audio file Sound2:Play() wait(120) Sound3:Play() wait(114) Sound4:Play() wait(100) Sound5:Play() wait(104) Sound6:Play() wait(120) Sound7:Play() wait(120) end
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
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 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 346073558, 183479225, 167337732, 175755911, 199372537, 247303851, 342274063 } 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. while true do 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 SoundInstance.SoundId = RandomSong -- Sets the ID property of the "Sound" instance to the song ID retrieved from the table SoundInstance:Play() -- Plays the song 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 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