Music overlaps when ending

I have this random music but when the music ends, it overlaps with another song.

local SS = game:GetService("SoundService")

local Songs = SS:WaitForChild("Music"):GetChildren()

while task.wait(1) do
	local Music = Songs[math.random(1, #Songs)]

	Music.Playing = true
	task.wait(1)
	Music.Ended:Wait()

end
1 Like

it happened because u put it ona while task.wait do, u need to use like this:

local SS = game:GetService("SoundService")

local Songs = SS:WaitForChild("Music"):GetChildren()

local isPlaying = false

while isPlaying = false do
	local Music = Songs[math.random(1, #Songs)]

	Music.Playing = true
      isPlaying = true

	Music.Ended:Wait()
      isPlaying = false
end

correcr me if im wrong

2 Likes

Wouldn’t that lag cause it’s continuously being false? Nevermind, i see it being = to true.

1 Like

corrected OP’s script and combined it with @How4rdy 's script

local SS = game:GetService("SoundService")

local Songs = SS:WaitForChild("Music"):GetChildren()

local function RandomSong()
    while true do
        if #Songs == 0 then return end 
        local MusicIndex = math.random(1, #Songs)
        local Music = Songs[MusicIndex]
        
        if Music:IsA("Sound") then
            Music:Play() 
            Music.Ended:Wait() 
        end
    end
end

RandomSong()
2 Likes

yeah my bad that shouldve worked, nice man.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.