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

How to add a random playlist of music?

Asked by 8 years ago

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.

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. Rallient 40 — 8y

1 answer

Log in to vote
0
Answered by
cfiredog 274 Moderation Voter
8 years ago

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.

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 Rallient 40 — 8y
0
Yes, lines 3-4 would go where lobmusic:Play() currently is in your original script. cfiredog 274 — 8y
0
Alright, thanks! This worked! Rallient 40 — 8y
0
Glad I could help. cfiredog 274 — 8y
Ad

Answer this question