Help with music system

Hello,

So I have this music system which works good but I would like to improve it since it’s really buggy at most times. Sometimes takes like 30 seconds to play the next song, sometimes doesn’t even play very randomly.

local Music_Sys: table = {Queue = {}, Currently_Playing = nil, Currently_Availible = {}}

Music_Sys.Songs = {
	[6015329876] = 'All I Want for Christmas Is You - Mariah Carey ',
	[4953440320] = 'EARFQUAKE - Tyler, The Creator',
	[2080386860] = 'Faygo Dreams - 6 Dogs',
	[4743392810] = 'Lil Jeep - Lil Peep ',
	[4824165619] = 'Magnolia - Playboi Carti',
}

local MarketplaceService = game:GetService('MarketplaceService')
local SoundScreen: Instance = game.Workspace.songScreen.Screen.SurfaceGui

local function refreshSounds(): nil
	for sound in pairs(Music_Sys.Songs) do
		table.insert(Music_Sys.Currently_Availible, sound)
	end
end


function Music_Sys.Next(): nil
	local NewSong: number
	print(#Music_Sys.Queue)
	if #Music_Sys.Queue == 0 then
		if #Music_Sys.Currently_Availible <= 1 then refreshSounds() end
		
		NewSong = Music_Sys.Currently_Availible[math.random(1, #Music_Sys.Currently_Availible)]
		table.remove(Music_Sys.Currently_Availible, table.find(Music_Sys.Currently_Availible, NewSong))
	else
		NewSong = Music_Sys.Queue[1]
		table.remove(Music_Sys.Queue, 1)
	end
	
	game.ReplicatedStorage.Remotes.SongPlay:FireAllClients(Music_Sys.Songs[NewSong])
	local sus = pcall(function(): nil
		Music_Sys.Sound.SoundId = string.format('rbxassetid://%d', NewSong)
	end)
	if not sus then Music_Sys.Skip()return end
	SoundScreen.songName.Text = Music_Sys.Songs[NewSong]
end

function Music_Sys.AddToQueue(id): nil
	table.insert(Music_Sys.Queue, tonumber(id))
	Music_Sys.Songs[id] = MarketplaceService:GetProductInfo(id, Enum.InfoType.Asset).Name
end

function Music_Sys.Skip(): nil
	if Music_Sys.Sound.IsPlaying then
		Music_Sys.Sound.TimePosition = Music_Sys.Sound.TimeLength
		wait()
		Music_Sys.Sound.TimePosition = 0
	end
end


function Music_Sys.Start(): nil
	local Sound: Instance = Instance.new'Sound'
	Sound.Parent = game.Workspace
	Music_Sys.Sound = Sound
	
	while true do
		wait(.1)
		Music_Sys.Next()
		Sound:Play()
		Sound.Ended:Wait()
		Sound:Stop()
	end
end


return Music_Sys

As you can tell, this is in a module.

I would like to know if I’m doing this the right way, the loop, skip and Request song.

Thanks!

1 Like

I wouldn’t use numbers as the indexes for the songs table, as that means it’ll be an array. I’d make them a string. and update the sting formatting for the asset id to just concatenate the string with the .. operator.

Why not? It’s working properly for me, there’s no issue there. I know I can get the song info using MarketplaceService:GetProductInfo but the song credit might be a little different.

GetProductInfo uses a 64-bit integer as the asset’s asset-ID, so I would keep it as a number, but instead make the songs table an array of tables, with each containing the sound’s sound-ID and its name.

Mucis_Sys.Songs = {
	{6015329876, 'All I Want for Christmas Is You - Mariah Carey '},
	{4953440320, 'EARFQUAKE - Tyler, The Creator'},
	{2080386860, 'Faygo Dreams - 6 Dogs'},
	{4743392810, 'Lil Jeep - Lil Peep '},
	{4824165619, 'Magnolia - Playboi Carti'},
}

Alright, now I get what you mean, thanks, I’ll go ahead and implement that.

Do you have any idea on how to improve this section?

function Music_Sys.Start(): nil
	local Sound: Instance = Instance.new'Sound'
	Sound.Parent = game.Workspace
	Music_Sys.Sound = Sound
	
	while true do
		wait(.1)
		Music_Sys.Next()
		Sound:Play()
		Sound.Ended:Wait()
		Sound:Stop()
	end
end

No, but you could clean the code up a bit by replacing the true in the loop statement with a wait(.1) and just remove the original wait statement altogether.