Music stops playing after second time playing

I want to achieve a working playlist script, the issue is that it stops playing after the second time.

Server script:

local MusicTable = {
	id1,id2,id3,etc
}
local MPS = game:GetService("MarketplaceService")
local Music = game.Workspace.Folder:WaitForChild("Music")

local OnPlayBoolean = true

while not Music.IsPlaying do
	if OnPlayBoolean then
		OnPlayBoolean = false
		local RandomMusic = MusicTable[math.random(#MusicTable)]
		Music.SoundId = "rbxassetid://"..RandomMusic
		Music:Play()
		if Music.Ended then
			print(MPS:GetProductInfo(RandomMusic).Name.." has started playing.")
		end
		Music.Ended:Wait()
		OnPlayBoolean = true
	end
end

Please ignore:

if Music.Ended then
      print(MPS:GetProductInfo(RandomMusic).Name.." has started playing.")
end
1 Like

Found a solution for it:

local MusicTable = {
	id1,id2,id3,etc
}
local MPS = game:GetService("MarketplaceService")
local Music = game.Workspace.Folder:WaitForChild("Music")

local OnPlayBoolean = true
local Wait_Between_Audio = 2.5 --this

while not Music.IsPlaying do
	if OnPlayBoolean then
		OnPlayBoolean = false
		local RandomMusic = MusicTable[math.random(#MusicTable)]
		Music.SoundId = "rbxassetid://"..RandomMusic
		wait(Wait_Between_Audio) --and this
		Music:Play()
		if Music.Ended then
			print(MPS:GetProductInfo(RandomMusic).Name.." has started playing.")
		end
		Music.Ended:Wait()
		OnPlayBoolean = true
	end
end
1 Like