Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a working, music shuffled playlist?

Asked by 6 years ago

There's this script I have that is supposed to make music play in shuffled order. The only problem once the first song is over, it plays the first song again without transitioning to the next one.

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

musicTable = --Making a table of music / your songs
{
    "rbxassetid://142305080",  -- 1 - Let's Groove
    "rbxassetid://671389213",  -- 2 - Can't Touch This
    "rbxassetid://609246843",  -- 3 - Take on Me
    "rbxassetid://1100823134", -- 4 - At Dawn
    "rbxassetid://681266896",  -- 5 - Safety Dance
    "rbxassetid://323743169",  -- 6 - Working for the Weekend
    "rbxassetid://442656036",  -- 7 - Don't you (Forget About Me)
    "rbxassetid://706699539",  -- 8 - Beat it
    "rbxassetid://838691861"   -- 9 - Hey you, you better keep walking!
}

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(1) -- 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

How can I fix this?

0
I have a fix for it. I'm testing it iRexBot 147 — 6y

1 answer

Log in to vote
0
Answered by
iRexBot 147
6 years ago

Your code to shuffle is useless pretty much as your code is just making the chosen song 1 every time it shuffles through.

Here is the fix c;

musicTable = --Making a table of music / your songs
{
    "rbxassetid://142305080",  -- 1 - Let's Groove
    "rbxassetid://671389213",  -- 2 - Can't Touch This
    "rbxassetid://609246843",  -- 3 - Take on Me
    "rbxassetid://1100823134", -- 4 - At Dawn
    "rbxassetid://681266896",  -- 5 - Safety Dance
    "rbxassetid://323743169",  -- 6 - Working for the Weekend
    "rbxassetid://442656036",  -- 7 - Don't you (Forget About Me)
    "rbxassetid://706699539",  -- 8 - Beat it
    "rbxassetid://838691861"   -- 9 - Hey you, you better keep walking!
}

local currentSong = 1 -- the 1st song plays
local sound = Instance.new("Sound") -- Creates a sound object
sound.Parent = game.SoundService -- Puts it in sound service so all players can hear it.


while true do
    print(currentSong)
    sound.SoundId = musicTable[currentSong]
    sound:Play()
    if currentSong~=#musicTable then -- Shuffles through the song now
        currentSong = currentSong+1
    else -- Resets the playlist.
        currentSong = 1
    end
    repeat wait() until sound.Playing==false 
end
0
Now it's messed up, whenever I join the game it starts playing the playlist. I attached this script to the Audio object with volume set to 0 but it still plays. I want this script to have connection to its Parent which is an Audio. ShadowNinja1080 6 — 6y
Ad

Answer this question