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.
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.