How would I make a music loop?

How would I make a script that makes like a music script? Ie: I have a folder in ReplicatedStorage and I have about 5 sounds which play songs. So how would I make a script where it would begin the first song and when that song ended it would start the next song?

2 Likes

Loop through a table. Example:

local sounds = path.to.folder:GetChildren()
while true do

for _,sound in pairs(sounds) do
      sound:Play()
      sound.Ended:Wait()
end
wait(1)
end
6 Likes

Use .Ended to detect sound ending. an example:

Sound5.Ended:Connect(function()
--do something like play another song
end)
1 Like

Not a huge deal, but you can actually use wait() as the condition in a while loop like this:

while(wait(x)) do
    print("Hello");

end

or, if you want to do something once before the condition is checked

repeat
    print("World!");

until(not wait(x))
1 Like

Pretty sure my method might be more efficient. It was just an example, they can obviously change it.

3 Likes

Adding on top TheWorstOne’s response, using wait(x) in code like that is bad practice when a solution that uses :Wait() exist.

3 Likes

Yes I know. In the original example, they have another wait statement at the end of the script, which is redundant when you can place it as the condition.

2 Likes

I think the easiest one would be @Andr_yy’s idea. Will that work finely?

Yes, it should. I tested it and it worked fine :+1: