Looping a function

I’m attempting to create a playlist, in which picks a random song every time a song ends. Unfortunately, it only plays 1 song, and after is has ended, it doesn’t play another song. What’s the concurrent issue here? I’m proceeding to assume that I may have used an inaccurate function, which is repeat wait until, but I’m not sure. Here’s the code:

local ids = {601790165, 1666515576, 2203122009, 3143083388, 1632522359, 587468803, 2683642139, 602136047, 2832773762, 1334793858, 706628304, 3035657191, 2890946507, 4755430395, 3743920540, 2992018267, 4595551862, 616542359, 4926033735, 2057045883, 4380429016, 3063659387, 372065073}

local function music()
    local se = Instance.new("Sound")
    se.Name = "BackgroundMusic"
    se.Parent = game.Workspace
    se.Volume = .9
    repeat wait()
    local chosenid = ids[math.random(1, #ids)]
    se.SoundId = "rbxassetid://"..chosenid
    se:Play()
    until se.Ended:Wait()
end
music()

repeat something until condition will stop running when condition becomes a truthy value. Ended:Wait() will always return a truthy value—the id of the song that was playing—so the first time a song ends the loop will stop. If you want to fix this, you could either do until se.Ended:Wait() and false (make it so the value can never be truthy), or put the .Ended:Wait() inside the loop itself and do until false.

2 Likes