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.
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)