Shuffled playlist

local plist = game.SoundService.Folder
local song = plist[math.random(1,#plist:GetChildren())]


while true do
	song:Play()
end

So what I am trying to do is make it so the songs play one after another without a specific order so people don’t hear the same song twice or more in a row. How would I do this quickly? My script only goes through one song, and I don’t know how to detect with the song is finished so I can play another.

You can do when the song is paused because when it’s finished it is also paused song:Paused

1 Like

Instead of using a while true do loop you can use these events:


https://developer.roblox.com/en-us/api-reference/class/Sound

Example:

sound.DidLoop:Connect(function(soundid,timeslooped)
-- play a different song
end)
1 Like
while true do
	local song = plist[math.random(1,#plist:GetChildren())]
	song:Play()
end

but how would i make it right here so it picks another song? i’m confused on how to use these

local plist = game.SoundService.Folder:GetChildren()

-- function for randomizing a table
local function FYshuffle(t)
	local j, temp
	for i = #t, 1, -1 do
		j = math.random(i)
		temp = t[i]
		t[i] = t[j]
		t[j] = temp
	end
end

local n = #plist + 1
local lastSongPlayed
while (#plist > 1) do -- doesn't work if there's only one song
	-- if at the end of playlist, shuffle playlist and reset 'n'
	if (n > #plist) then
		repeat
			FYshuffle(plist)
		-- shuffle again if the next song is same as last song.
		until (plist[1] ~= lastSongPlayed)
		n = 1
	end
	-- play the next song and increment 'n'
	lastSongPlayed = plist[n]
	plist[n]:Play()
	plist[n].Ended:Wait()
	n = n + 1
end
3 Likes

It doesn’t play a song after the original. I just tested and it only plays one song and no more. There are a total of 6 songs in the playlist yet it only plays one.

It works when I test it in my studio. Did you make sure all your sounds have their Looped property set to false?

Yep.

image

Every single method I’ve tested apparently only plays 1 song or 2 at the same time, can you show me what your explorer looks like too?

My folder was set up the same way as yours and I had one script in the workspace containing only the code I posted earlier.

My script is called script in serverscriptservice.

Can i see the properties of one of the sounds you are using?

Ok I think I see the issue. It works in the “Test” mode in studio but in a simulated game the .Ended event is not replicating from the server to the client.

How do we fix that? It doesn’t work in studio for me either btw but that might because I have team create on.

You could make the whole thing client sided. Moving the code into a local script inside StarterPlayer.StarterPlayerScripts might fix it.

1 Like

example.rbxl (22.7 KB)