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

How do I fix a looping music playlist in this script?

I have script where you have a GUI button, inside of it, there is an "Audio", and inside the "Audio" there is a script. When you click it, you have a playlist set of music that was intended to be played in a shuffled order. Here's how the script inside the "Audio" looks like;

musicTable = --Making a table of music / your songs
{
    
    "rbxassetid://142305080", -- 1 - Let's Groove

    "rbxassetid://142386715", -- 2 - Can't Touch This
    
    "rbxassetid://276873987"  -- 3 - Noob Song
  
}

--Instead of you doing wait(), we can use the event "Ended"

while true do
    math.randomseed(tick()) --Creates a new seed every time(basically makes more random)
    local randomSong = musicTable[math.random(1, #musicTable)]
    wait(5) -- Just creating a small pause before a random song plays
    script.Parent.SoundId = randomSong --Choose song
    script.Parent:Play() --Play song
    script.Parent.Ended:wait() -- Waiting until the song has finished
end

The only problem, after it plays one song, it still keeps repeating! How do I make it so that when it plays one song, it transitions to another in a shuffled order without looping?

1 answer

✓ Accepted answer

Whoever gave you this line math.randomseed(tick()) --Creates a new seed every time(basically makes more random) is wrong. You are generally only supposed to call randomseed once on the server and once on the client (for all scripts). Ironically, due to a flaw in Lua's randomseed implementation, calling it repeatedly so close to each other will remove randomness, as the first random number you get will very likely be the same as the last one. It's a simple fix:

math.randomseed(tick())
math.random() -- throw out first not-very-random value (known limitation of lua)

local musicTable = --Making a table of music / your songs
{
    "rbxassetid://142305080", -- 1 - Let's Groove
    "rbxassetid://142386715", -- 2 - Can't Touch This
    "rbxassetid://276873987"  -- 3 - Noob Song
}

while true do
    local randomSong = musicTable[math.random(1, #musicTable)]
    wait(5) -- Just creating a small pause before a random song plays
    script.Parent.SoundId = randomSong --Choose song
    script.Parent:Play() --Play song
    script.Parent.Ended:wait() -- Waiting until the song has finished
end

However, that doesn't do exactly what you want -- you asked for everything to be shuffled, such that each song plays exactly once before you re-shuffle and do it again. Here's a simple shuffle function:

function Shuffle(list)
	local index
	for i = 1, #list-1 do
		index = math.random(i, #list)
		list[i], list[index] = list[index], list[i]
	end
end

The idea is that we could shuffle your musicTable and then play each song in the new random order, then re-shuffle and keep going. However, imagine if the first random order was {2, 1, 3} and the second random order was {3, 1, 2} -- the user would hear song #3 two times in a row! We can fix this by changing the shuffle function to not choose a last element for the first one:

local index = math.random(1, #list - 1) -- choose an item not from the end
list[1], list[index] = list[index], list[1] -- switch with first item
for i = 2, #list-1 do -- do regular shuffle algorithm starting at 2nd item
	index = math.random(i, #list)
	list[i], list[index] = list[index], list[i]
end

Replacing 'list' with 'musicTable', we can now merge that into your script:

math.randomseed(tick())
math.random() -- throw out first not-very-random value (known limitation of lua)

local musicTable = --Making a table of music / your songs
{
    "rbxassetid://142305080", -- 1 - Let's Groove
    "rbxassetid://142386715", -- 2 - Can't Touch This
    "rbxassetid://276873987"  -- 3 - Noob Song
}

while true do
	--Shuffle
	local index = math.random(1, #musicTable - 1) -- choose an item not from the end
	musicTable[1], musicTable[index] = musicTable[index], musicTable[1] -- switch with first item
	for i = 2, #musicTable - 1 do -- do regular shuffle algorithm starting at 2nd item
		index = math.random(i, #musicTable)
		musicTable[i], musicTable[index] = musicTable[index], musicTable[i]
	end
	--Play music
	for i = 1, #musicTable do
		wait(5) -- Just creating a small pause before a random song plays
		script.Parent.SoundId = musicTable[i] --Choose song
		script.Parent:Play() --Play song
		script.Parent.Ended:wait() -- Waiting until the song has finished
	end
end

3 comments

0

It doesn't even work at all anymore! How can I fix this now!

0

Oh, sorry about that - I forgot to rename 'list' to 'musicTable' in the script. I've updated it and tested it and it is working now.

0

Yeah, I already figured it out too before you replied to me. Thanks anyways!

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