I need help with my music script

I made a script to play music in my game, the problem is it will only play one song. can someone help :face_holding_back_tears:

----[VARIBLES]----
local musicOBJ = Instance.new("Sound")
musicOBJ.Name = "soundfr"
musicOBJ.Parent = script

----[MUSIC]----
local musicList = {
	"http://www.roblox.com/asset/?id=1842627030", -- Music 1
	"http://www.roblox.com/asset/?id=1841614322", -- Music 2 
	"http://www.roblox.com/asset/?id=1836711546", -- Music 3 
	"http://www.roblox.com/asset/?id=9040183974", -- Music 4 
	"http://www.roblox.com/asset/?id=1836729143", -- Music 5 
	"http://www.roblox.com/asset/?id=9048046591", -- Music 6
	"http://www.roblox.com/asset/?id=1841487943", -- Music 7
	"http://www.roblox.com/asset/?id=1836729170", -- Music 8
	"http://www.roblox.com/asset/?id=1836681160", -- Music 9 
	"http://www.roblox.com/asset/?id=9413914950"  -- Music 10
}

----[FUNCTIONS]----
local function music()
	local x = math.random(1, 10) -- Change depending on how many song you added
	musicOBJ.SoundId = musicList[x]
	musicOBJ:Play()
end

----[MUSIC PLAYER]----
while not musicOBJ.Playing do
	music()
end
----[VARIBLES]----
local musicOBJ = Instance.new("Sound")
musicOBJ.Name = "soundfr"
musicOBJ.Parent = script

----[MUSIC]----

local musicList = {
	"http://www.roblox.com/asset/?id=1842627030", -- Music 1
	"http://www.roblox.com/asset/?id=1841614322", -- Music 2 
	"http://www.roblox.com/asset/?id=1836711546", -- Music 3 
	"http://www.roblox.com/asset/?id=9040183974", -- Music 4 
	"http://www.roblox.com/asset/?id=1836729143", -- Music 5 
	"http://www.roblox.com/asset/?id=9048046591", -- Music 6
	"http://www.roblox.com/asset/?id=1841487943", -- Music 7
	"http://www.roblox.com/asset/?id=1836729170", -- Music 8
	"http://www.roblox.com/asset/?id=1836681160", -- Music 9 
	"http://www.roblox.com/asset/?id=9413914950"  -- Music 10
}

----[FUNCTIONS]----
local function music()
	local x = musicList[math.random(1, #musicList)]
	musicOBJ.SoundId = x
	musicOBJ:Play()
end

----[MUSIC PLAYER]----
while not musicOBJ.Playing do
	music()
end

You needed to pick from the table like this and just use x on SoundId rather than musicList[x]

musicOBJ:Play() does not yield, which means that in your while loop’s condition will be checked immediately after you start playing the music (and in turn ending the while loop)

In order to fix this you can add something like musicOBJ.Ended:Wait() to the while loop.

while true do
	music()
    musicOBJ.Ended:Wait() -- Wait for sound to finish
end

Add musicOBJ.Ended:Wait() in your script

musicOBJ:Play()
musicOBJ.Ended:Wait()

this makes the script wait until the current music has finished playing before moving on to the next song.

Let’s elaborate this! First by looking here!

----[MUSIC PLAYER]----
while not musicOBJ.Playing do
	music()
end

The issue here is that the while loop is not infinite here. Additionally, a while loop alone also means only one parameter. What if you want to restart it at a certain time, stop at a certain time? Here is how to make it loop approximately forever until a condition is met for whatever reason!

local function playMusic()
	local musicIndex = math.random(1, 10) -- Change depending on how many song you added
	soundPlayer.SoundId = musicList[musicIndex] -- renaming musicOBJ to soundPlayer for improved semantics
	soundPlayer:Play()
	soundPlayer.Ended:Wait() -- waits for the sound to end
end

local function startMusicPlayer()
	while canPlay do -- assume canPlay is a boolean variable
		playMusic()
	end
end

-- To play music, just call the function!
startMusicPlayer()

…but wait, I want to add more music and it only picks the first ten of them because of math.random(1, 10). If you want to scale the number of sounds, the 10 should be replaced with #musicList. The # operator counts how many indices there are in the table.

hey man, i ran into a similar problem before but this script works perfectly.

i think it’s easier if you put your songs in a folder in the sound service like this but yeah:
image

this plays a random song and it’ll play a random song after that. it’ll loop forever!

local songs = game:GetService("SoundService").songs

while wait() do -- u have to add wait or rblx studio crashes
	for i, song in pairs(songs:GetChildren()) do --getting all the sounds from folder
		if song:IsA("Sound") then -- if its a SOUND then:
			local randomNo = math.random(1, #songs:GetChildren()) -- choosing a random number out of how many songs are in the folder
			local randomSong = songs:GetChildren()[randomNo] -- plays a random song from all the songs
			randomSong:Play() -- play song
			randomSong.Ended:Wait() -- wait until the song has finished, then it'll play another!
		end
	end
end

its not that it wont select the music, just it would play more than one. more likely a problem in the Music Player section

What do you mean by assume canPlay is a boolean varible? It doesnt do anything

You’ll have to declare it. A boolean is the term for the collective of true and false states.

local canPlay = true