How to make a music shuffler?

I need a script that shuffles the soundId on a sound object

local Songs = {
	"rbxassetid://9039415289";
	"rbxassetid://1839867470";
	"rbxassetid://1836720789";
	"rbxassetid://1841031799";
}

2 Likes
math.random(1, #Songs)

the #Songs gives you the amount of values in the table
choose a random number and use that number for index

1 Like

here’s how you can do it

local Songs = {
	"rbxassetid://9039415289";
	"rbxassetid://1839867470";
	"rbxassetid://1836720789";
	"rbxassetid://1841031799";
}

local Sound = -- Path to your sound object
local RandomID = Songs[math.random(1,#Songs)] -- gets a random value off the "Songs" Table
Sound.SoundId = RandomID
1 Like

how do i make it so it changes every time the song ends?

local songs = {} -- ...
local currentSong;

local function chooseSong()
       local song = currentSong
       while (song == currentSong) do
              song = math.random(1, #songs)
       end
       return song
end

task.spawn(function()
      while (1) do
            currentSong = chooseSong()
            local songID = songs[currentSong]
            soundObject:Stop()
            soundObject.SoundId = songID
            soundObject:Play()
            soundObject.Ended:Wait()
      end
end)

this will also make sure the same song isnt played twice in a row

3 Likes

Does looped need to be enabled for it to work?

1 Like

Nope, you don’t need it to be looped
The code works as is :+1::+1:

here you go, this will also prevent the song from being picked twice in a row.

local Songs = {
	"rbxassetid://9039415289";
	"rbxassetid://1839867470";
	"rbxassetid://1836720789";
	"rbxassetid://1841031799";
}

local Sound =   -- Path to your sound object
local PreviousSong
while wait() do
	local RandomID = Songs[math.random(1,#Songs)] -- gets a random value off the "Songs" Table
	if tostring(RandomID == PreviousSong) then repeat wait() RandomID = Songs[math.random(1,#Songs)] until tostring(RandomID ~= PreviousSong)
	Sound.SoundId = RandomID
	Sound:Play()
	Sound.Ended:Wait()
	PreviousSong = RandomID
	end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.