How would I make a new song play after another song ends?

hey! so i have a randomized song system which works fine but when the song ends, it just goes silent. is there anyway to make a new song play after one song ends? here is my script:

local baba = MUSIC:GetChildren()
local layla = game.ReplicatedStorage.Layla
local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Layla")
local sounds = {	
	
	MUSIC:WaitForChild("FreakTheFreakOut"),
	MUSIC:WaitForChild("DiamondAndPearl"),
	MUSIC:WaitForChild("AdoreYouHarryStyles"),

}
	



local song 
function playRandomSong()
	song = sounds[math.random(#sounds)]
	song:Play()
	print("haha")
	remote:FireAllClients(song)
end

if song.IsPlaying == false then
	function playRandomSong()
else
	--???
	end
end


remote.OnServerEvent:Connect(function(player) 
	remote:FireClient(player, song) --manually tell the player the song
end)

playRandomSong()

i started an else statement thing but im not sure what to put in else to make the song keep playing if there is something already playing (if that makes sense) or if im even supposed to use an else statement thing at all?

2 Likes

You don’t really have to have anything to complicated, Just a while loop that checks if the song is playing and if it isn’t then it plays a song, so something like this

local Music = game.SoundService:WaitForChild("Sound")
local RunService = game:GetService("RunService")
local Songs = {
-- Song Id Numbers
1234567,
7654321,
-- etc
}
while true do
	if Music.IsPlaying == false then -- Checks if a song is playing
	local Song = table.unpack(Songs, math.random(#Songs)) -- Picks Song
		print(Song)	
		Music.SoundId = "rbxassetid://"..Song
		Music.Playing = true 
	end
	wait(2.5) -- Every 2.5 seconds it checks if the song is still playing
end
3 Likes

Sounds have documentation that you can reference, you should look there and consult documentation first for any other technical troubles you may run into. Ended for an event-driven approach (you should scarcely use loops as they may wait more time than actually necessary).

2 Likes

Sound.Ended:Wait() --or connect
local newsound = soundsTable[math.random(1,#soundTable)]

1 Like
-- put it in a loop if you want 
for _,song in pairs(songs) do
song:Play()
song.Ended:Wait()
end
2 Likes

adding this made it not randomized, rather making the song on top of the list play first (update, it does make it play the next song, is there any way to make it work with the randomization?)

You can randomize the list of songs

im kind of confused on how like what do i change in your code to add the math.random portion from mine

The below code assumes you have your sounds named as numbers, eg. Sound 1 is named “1”, sound 2 is named “2” and so on.

local RNG
local Songs = game:GetService("ReplicatedStorage"):GetChildren() -- You are using ReplicatedStorage, right?
local NewSong

while Songs do
	RNG = math.random(X,Y) -- Where X is the first song in storage, Y is the last.
	NewSong = game:GetService("ReplicatedStorage"):FindFirstChild("RNG")
	if NewSong and NewSong.Name ~= tostring(RNG) then
		NewSong:Play()
		NewSong.Ended:Wait()
	elseif not NewSong then
		NewSong:Play()
		NewSong.Ended:Wait()
	end
end
function playRandomSong()
	while true do 
		song = sounds[math.random(1, #sounds)]
		song:Play()
		song.Ended:Wait()
		remote:FireAllClients(song)
	end
	print("haha")

end


remote.OnServerEvent:Connect(function(player) 
	remote:FireClient(player, song) --manually tell the player the song
end)

playRandomSong()

hey so i got it to be random, however now the remote doesnt fire like its supposed to after the song ends

if not song.IsLoaded then
	song.Loaded:Wait()
end
song:Play()

Although the sounds are being instanced by the server so preloading shouldn’t be an issue.

Add prints to debug & check the console for any errors.