Is there a better way to loop this music script?

I want to know if there is a better way for me loop this music script. It is a local script in StarterPlayerScripts. I have done some research and haven’t found a way. I was thinking maybe doing this by using RunService? Any help is appreciated :grinning:.

Here is my script:

local Music1 = "http://www.roblox.com/asset/?id=2446563125"
local Music2 = "http://www.roblox.com/asset/?id=1362952031"
local Music3 = "http://www.roblox.com/asset/?id=6104227669"
local Music4 = "http://www.roblox.com/asset/?id=5436430946"
local Music5 = "http://www.roblox.com/asset/?id=3183234427"


local CurrentMusic = Instance.new("Sound")
CurrentMusic.Archivable = true


CurrentMusic.Parent = script
CurrentMusic.Pitch = 1
CurrentMusic.Looped = false
CurrentMusic.Name = "Song"


while true do

	CurrentMusic.SoundId = Music1 
	CurrentMusic.Volume = 0.08
	CurrentMusic:Play()
	CurrentMusic.Ended:Wait()

	CurrentMusic.SoundId = Music2
	CurrentMusic.Volume = 0.08
	CurrentMusic:Play() 
	CurrentMusic.Ended:Wait()

	CurrentMusic.SoundId = Music3
	CurrentMusic.Volume = 0.07
	CurrentMusic:Play() 
	CurrentMusic.Ended:Wait()

	CurrentMusic.SoundId = Music4
	CurrentMusic.Volume = 0.12
	CurrentMusic:Play() 
	CurrentMusic.Ended:Wait()

	CurrentMusic.SoundId = Music5
	CurrentMusic.Volume = 0.08
	CurrentMusic:Play() 
	CurrentMusic.Ended:Wait()	
end

IN MY OPINION

it’s better if u do it in server side

You can put all of the sounds into a folder (in the explorer), use :Get children() to get each sound, and use an in pairs loop to loop them.

In this example, the folder will be named “music”
(I am on mobile at the moment so the script may be a bit off)

local MusicFol = game.Workspace:WaitForChild("music")
local songs = MusicFol:GetChildren()

while true do
for i, CurrentMusic in pairs(songs) do
	CurrentMusic.Volume = 0.12
	CurrentMusic:Play() 
	CurrentMusic.Ended:Wait()
end
end

With this, you won’t have to add any extra scripts when you put a new song in the folder

I have it set up to be able to shut down by client with mute music button so if they clicked it wouldn’t that shut down for the whole server?

If I wanted to set the volume of each music to a different volume, could I just not change the volume of what the music already is?

I suppose you could. I just put the volume thing in there because it was part of the original script. It should work without it

local Musics = {
	"http://www.roblox.com/asset/?id=2446563125",
	"http://www.roblox.com/asset/?id=1362952031",
	"http://www.roblox.com/asset/?id=6104227669",
	"http://www.roblox.com/asset/?id=5436430946",
	"http://www.roblox.com/asset/?id=3183234427"
}

local CurrentMusic = Instance.new("Sound")
CurrentMusic.Archivable = true


CurrentMusic.Parent = script
CurrentMusic.Pitch = 1
CurrentMusic.Looped = false
CurrentMusic.Name = "Song"
CurrentMusic.Volume = 0.12

local i = 0

while true do
	repeat
		i +=1
		CurrentMusic.SoundId = Musics[i]
		CurrentMusic:Play()
		CurrentMusic.Ended:Wait()
	until (i == #Musics)
	i = 0
end

This seems to work perfectly! For anyone wondering here is the script:

local Songs = script:GetChildren()

while true do
	for i, CurrentMusic in pairs(Songs) do
		CurrentMusic:Play() 
		CurrentMusic.Ended:Wait()
	end
end
local SoundIds = {2446563125, 1362952031, 6104227669, 5436430946, 3183234427}
local Sound = Instance.new("Sound")
Sound.Parent = script
Sound.Name = "Song"

while true do
	for i = 1, #SoundIds do
		Sound.SoundId = "rbxassetid://"..SoundIds[i]
		if not Sound.IsLoaded then
			Sound.Loaded:Wait()
		end
		Sound:Play()
		Sound.Ended:Wait()
	end
end

Here’s a better solution which is more dynamic and doesn’t rely on creating 5 “Sound” instances, this will also allow you to add additional audio asset IDs to the “SoundIds” array.