Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to add a random playlist of music?

So I've been wondering how exactly would I add a random playlist of music. I have a folder in workspace called Music, and then under that folder two folders, with one being called lobby music, and the other called map music. I'm more concerned about the lobby music, because once I figure out how to do this, I can easily do it with the map music.

Under the Lobby music folder, there are three sounds and they all have the sound IDs.

(This is part of the script, it is a round script).

local replicatedstorage = game:GetService('ReplicatedStorage')
local status = replicatedstorage:WaitForChild('InfoValue')
local mapstorage = game.Workspace:WaitForChild('mapStorage')
local lobmusic = game.Workspace.Music.LobbyMusic
while true do

while game.Players.NumPlayers < 1 do
	status.Value = 'Two Players are needed to start a round'
	repeat wait(2) until game.Players.NumPlayers >= 2
end

wait(3)
lobmusic:Play()
for i = 30,0,-1 do
	status.Value = 'Intermission ('..i..')'
	wait(1)
end

It works, I just wonder how I would make it to where the music randomizes each time.

1 comment

0

I was also testing something where it does the 'Two Players are neede to start a round' so I could test it myself, but I didn't change the >= 2 part. Just ignore that.

1 answer

✓ Accepted answer

You will need to use 'math.random' to achieve the desired outcome:

local lobmusic = game.Workspace.Music.LobbyMusic:GetChildren()  --Gets all sounds in the folder

local LMusic = lobmusic[math.random(1,#lobmusic)]  --Picks a random song
LMusic:Play() --Plays the song that was randomly chosen

Note: If you want to play and stop music throughout multiple functions, I would suggest using a global variable rather than a local one. If you want me to explain further how to turn it into a global variable then feel free to comment below.

Don't forget to hit the accept answer button if this answers your question. If you have any questions, feel free to comment below.

4 comments

0

Alright, so for lines 3-4, would that go where Lobmusic:Play() is at, or just under the local lobmusic = game.Workspace.Music.LobbyMusic:GetChildren() part

0

Yes, lines 3-4 would go where lobmusic:Play() currently is in your original script.

0

Alright, thanks! This worked!

0

Glad I could help.

This discussion is preserved as it was. New answers, comments, and votes are closed.