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!