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 playlist go in a proper, shuffled way?

There's this script I have which it's parent is a Sound object. It gives you many songs but however they go in order. I want to make this playlist in shuffled order. I asked for help before and one guy gave me script that ended up having the first song in the list play again for the second time instead of transitioning to the next one.

while true do
	script.Parent.SoundId= "rbxassetid://142304703" --1 Fortunate Son
	script.Parent:Play()	
	script.Parent.Ended:wait()
	wait(1)
	script.Parent.SoundId= "rbxassetid://142304778" --2 I Can See Clearly Now
	script.Parent:Play()
	script.Parent.Ended:wait()
	wait(1)
	script.Parent.SoundId= "rbxassetid://142304968" --3 Imagine
	script.Parent:Play()
	script.Parent.Ended:wait()
	wait(1)
	script.Parent.SoundId= "rbxassetid://142305080" --4 Lets Groove
	script.Parent:Play()
	script.Parent.Ended:wait()
	wait(1)
	script.Parent.SoundId= "rbxassetid://142305170" --5 Mercy Me
	script.Parent:Play()
	script.Parent.Ended:wait()
	wait(1)
end

How do I make this playlist go in shuffled order and not have the same songs repeated again for while until it it gets randomly picked again?

1 answer

This script can be cleaned up a lot

For a start, you can make your whole long loop into a function which is useful as it can be called endlessly saving you the need to copy and paste

-- This function plays the sound ID first argument sent
local function play(id)
	script.Parent.SoundId = "rbxassetid://"..id  -- Those two dots mean the string is being combined
	script.Parent:Play()
	script.Parent.Ended:wait()
end

If you want them to play in an order then you can put them in a table which is easily referenceable later

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

Now that you've got your two essentials completed, you can combine them using a loop which calls the play function on each item in the table

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

In the end you have a callable function which will play the music ID of your choice, a table full of your music ID's you want to be played, and a loop that will go through each item in the table

local musicTable = { 142304703, 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

3 comments

0

Basically, just make all your music ID's go into a table. Then, have a random selector to choose a random one of them to play. To avoid immediate song repetition, make the script remove the most recent outcome from the table until the next song plays, then re-insert it.

0

If you're planning to use math.random() to make the next song to play chosen randomly, it'll play the same songs in the same order. You can use math.randomseed() to stop this behaviour. Read more about that here: http://wiki.roblox.com/index.php?title=Random_numbers

0

So the script that he gave me, is it shuffled playlist or it goes in order?

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