My Song Script is not working

I want my script to play a random song from my table of songs forever but after the second song, it stops playing.

local Songs = script.Parent:WaitForChild("Songs"):GetChildren()
local FirstRandomSong = Songs[math.random(1,#Songs)]

local Debounce = false

local CurrentSong = FirstRandomSong

CurrentSong:Play()

CurrentSong.Ended:Connect(function()
	print("song ended")

	local RandomSong = Songs[math.random(1,#Songs)]
	
	CurrentSong = RandomSong
	RandomSong:Play()
end)
2 Likes

On the second to last line, perhaps try changing RandomSong:Play() to CurrentSong:Play() and share the results, if any.

1 Like

There was no difference, it stopped after the second song.

Have you tried using a while loop?

while wait(1) do
-- code here
end
1 Like

Maybe try making your own function to do this rather than CurrentSong.Ended
ex:

local songs = script.Parent:WaitForChild("Songs"):GetChildren()

local previousSong

local function chooseSong()
    local chosenSong = songs[math.random(1, #songs)]
    if chosenSong ~= previousSong then
        previousSong = chosenSong

        chosenSong:Play()

        wait(chosenSong.TimeLength)

        chooseSong()
    else
        chooseSong()
    end
end

chooseSong()

Edit: This is assuming none of the audios have Looped set to true.

2 Likes

The problem is I have a portion of the script that pauses the music and I don’t think it would still work, Here is the whole script:

local Songs = script.Parent:WaitForChild("Songs"):GetChildren()
local FirstRandomSong = Songs[math.random(1,#Songs)]

local Debounce = false

local CurrentSong = FirstRandomSong

CurrentSong:Play()

CurrentSong.Ended:Connect(function()
	print("song ended")

	local RandomSong = Songs[math.random(1,#Songs)]
	
	CurrentSong = RandomSong
	CurrentSong:Play()
end)

game.ReplicatedStorage.Band.LobbyMusic.OnServerEvent:Connect(function(player, Playing)
    if Playing == true then
	
        for _,song in pairs(Songs) do
            if song.Playing == true then
	
                for i = .1, 0 ,-0.01 do
                    wait(.05)
                    song.Volume = i 
                end

                song:Pause()
                CurrentSong = song

            end
        end

	elseif Playing == false then
        if CurrentSong then
	
            CurrentSong:Resume()

	        for i = 0, .1 ,0.01 do
				wait(.05)
	            CurrentSong.Volume = i 
	        end
        end
    end
end)

Maybe we do have to use the .Ended event… Maybe try this

local songs = script.Parent:WaitForChild("Songs"):GetChildren()

local previousSong
local chosenSong

local function chooseSong()
    chosenSong = songs[math.random(1, #songs)]
    if chosenSong ~= previousSong then
        previousSong = chosenSong

        chosenSong:Play()

        chosenSong.Ended:Wait()

        chooseSong()
    else
        chooseSong()
    end
end

-- pausing functionality.

chooseSong() -- run this at the end of your script

Edit: It may also be worth adding an if statement inside the RemoteEvent listener to make sure chosenSong has been selected yet (~= nil)

2 Likes

Does “:Wait()” just wait until the event is triggered and then continues the script?

Yes. 30 char

1 Like

It works! thank you so much for your help!

2 Likes