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:

01local Sound1 = game.Workspace.Sound1
02local Sound2 = game.Workspace.Sound2
03local Sound3 = game.Workspace.Sound3
04local Sound4 = game.Workspace.Sound4
05local Sound5 = game.Workspace.Sound5
06local Sound6 = game.Workspace.Sound6
07local Sound7 = game.Workspace.Sound7
08 
09Assets = {346073558,183479225,167337732,175755911,199372537,247303851,342274063}
10 
11for _, asset in ipairs(Assets) do
12     game:GetService("ContentProvider"):Preload("http://www.roblox.com/asset/?id=" .. asset)
13end
14 
15while true do
View all 30 lines...

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 — 8y
0
Updated my comments on my answer. :P TheeDeathCaster 2368 — 8y

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

01local 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
02local 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 
12math.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 
14while 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
19end -- 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 — 8y
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 — 8y
0
The 'SoundPlay' child/ object doesn't have to be the exact name; it's just a reference for a 'Sound' object. TheeDeathCaster 2368 — 8y
0
So why isn't it working? Nik1080 24 — 8y
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 — 8y
Ad

Answer this question