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

How do I make this script play music In order?

Asked by 5 years ago

So I made this script and it's only playing 1 song. I want it to play in order. So can anyone help me?

local songs = {
    279207008,
    1610954983,
    1610958262,
    1610989227,
    1610995500,
    1611013339,
    1611019115,
    1611026677,
    1611030715,
    1611033388
}

while true do
    wait(script.Parent.TimeLength)
    for i,v in pairs(songs) do
        script.Parent.SoundId = "rbxassetid://"..v; script.Parent:Play()
        wait(script.Parent.TimeLength)
    end
end

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

As you have a song id array you should use a numerical loop and not a generic for loop which can access in any order.

There are some other changes that you can make to help your code. First use event if there is one do not rely on things like wait() and second you are using script.Parent a lot so it is better to make a variable as you use the same ound instance each time you play a song.

To wait until the sound has finished playing you can use the event Ended and use the function :Wait() which will wait intil the event is fired and pass back any argument with the given event.

Example

local songs = {
    279207008,
    1610954983,
    1610958262,
    1610989227,
    1610995500,
    1611013339,
    1611019115,
    1611026677,
    1611030715,
    1611033388
}

-- make a variable of the sound since it is used multiple times and will not change
local soundObj = script.Parent
while true do
    for i=1, #songs do
        -- access the song id at the array index
        soundObj.SoundId = "rbxassetid://" .. tostring(songs[i])
        soundObj:Play()
        soundObj.Ended:Wait() -- wait for the song to finish playing
    end
end

I hope this helps.

0
Thanks. ixUltraz 28 — 5y
Ad

Answer this question