Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Randomizing a Music "Playlist?"

Asked by
Nik1080 24
8 years ago

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?

0
You could use the math.random function, and have the returned number retrieve the position asset in the table. "local Song = tbl[math.random(1, #tbl)]" TheeDeathCaster 2368 — 8y
0
but what about the wait? I dont think you can use those in math.random Nik1080 24 — 8y
0
No, you can't, but that's not what I mean w/ my comment. :P W/ the waits, you can still use them, but as I said in my answer, it gets pretty complicated. If you wish, I could go into detail about how you can accomplish this, but it may be a bit overloading. ;-; TheeDeathCaster 2368 — 8y
0
Like the comment above said, you could use math.random(). I'd also like to add that you don't really need all of those sound variable and should just store them in an array/table along with their individual length. It'd look something like: local s = {{id,length},{id,length},{id,length}} To load them you'd just do local ran = math.random(1,#s) and then load the asset by doing s[ran][1] and telling Meltdown81 309 — 7y
0
Updated my comments on my answer. :P TheeDeathCaster 2368 — 7y

1 answer

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

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

0
Tried that, but for some reason it wont work. You think its because the songs are not loading? Nik1080 24 — 8y
0
The songs may not have loaded, b/c there's no preload w/in this code; however, I think they're supposed to play after they load, even if they weren't preloaded. Try putting prints between the lines of code, and see which one prints; that may help us find the answer. TheeDeathCaster 2368 — 8y
0
Also, try going in-game, or do a studio test-run & see what happens. TheeDeathCaster 2368 — 8y
0
I keep getting this error: Infinite yield possible on 'Workspace:WaitForChild("SoundPlay") Nik1080 24 — 7y
View all comments (4 more)
0
That's warning you that if there's no 'SoundPlay' that's supposed to be in the parent, then it's going to stay yielding until the child/ object's finally in the parent. TheeDeathCaster 2368 — 7y
0
The 'SoundPlay' child/ object doesn't have to be the exact name; it's just a reference for a 'Sound' object. TheeDeathCaster 2368 — 7y
0
So why isn't it working? Nik1080 24 — 7y
0
Are you receiving any errors? If you don't already, have the Output enabled & run the script, as that may give us an idea of what the problem is. TheeDeathCaster 2368 — 7y
Ad

Answer this question