Hello Everyone, so when making my game Clash Of Roblox version Alpha 1.9, I thought of a process of making a playlist, something more efficient to not only write but have it actually function.
This is in a local script, inside StarterGui but will function in StarterPlayer.
Every player can get different songs playing for them in the background, and the script can be retrofitted with an option to disable it if needed.
It is even able to function if it is Disabled and then Re-Enabled by other scripts.
If you have any thoughts to make it more efficient, please comment down below.
local Music = {script.Song1, script.Song2} --More songs can be added here.
function MusicPlay()
local MusicChoice = math.random(1,#Music)
Music[MusicChoice]:Play() --Plays it.
end
for A,B in ipairs(Music) do --Checks all songs to see which one triggers a .Ended event
B.Ended:Connect(function()
MusicPlay() --Loops back to the function()
end)
end
Music[2]:Play()--Jumpstart line, choose any of the songs in the table to play first, and every player that joins will hear it first.
PS: If Anyone notice’s that i’m posting a similar topic to this, please point it out, since I haven’t been able to spot any topics related to this whilst surfing the net.
PPS: Is this script valid to put in #resources:community-resources ? And if so, how?
From what i can see, this traditional loop goes in a certain order right? Like from song 1 all the way to song 2 or 3 etc?
The idea of the playlist was add an element of random chance, at least so the next song that plays isn’t predictable by players (Yes there are some players who are nit-picky about it )
And ipairs() kinda works out faster too. But hey, whats your reasoning behind it?
I guess ipairs is actually faster in luau. In traditional lua, it is much slower. I did some benchmarks and ipairs is suprisingly 50% faster than a traditional loop.