Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make this script be shuffled music playlist?

There's this script I have that makes play music. The parent of the script is Sound object that allows the ability of Sound to play more than one song.

local musicTable = 
{   
	142304703,    --- efwfe
	142304778,    
	142304968, 
	142305080, 
	142305170
}

local function play(id)
    script.Parent.SoundId = "rbxassetid://"..id
    script.Parent:Play()
    script.Parent.Ended:wait()
end

for _, soundID in pairs(musicTable) do
    play(soundID)
end

The only problem, it goes into order random than shuffled which I want it to be. How do I make so that it doesn't play the songs in order and that it wouldn't repeat the song again after it played which I had experience when I had scripts that made song list randomized.

1 answer

You can use math.random() to pick a random index in your table. From there, once you play the id associated from that index, you can then remove it from the table options for the next one to be played using table.remove().


function play(id)
    script.Parent.SoundId = "rbxassetid://"..id
    script.Parent:Play()
    script.Parent.Ended:wait()
end



while wait() do
	local musicTable = {142304703, 142304778, 142304968, 142305080, 142305170}
	
	
	for i = 1, #musicTable do 
		local Random = musicTable[math.random(1, #musicTable)]
		play(musicTable[Random])
		table.remove(musicTable, Random)
	end
end

1 comment

0

Now it doesn't work at all!

This discussion is preserved as it was. New answers, comments, and votes are closed.