Help with sound

Hello everyone,

I am trying to make a audio player (Server sided), but I cant seem to get it to work.
Below is my code

songtable = {7698151169,5140419602,465694735,859998992,402306149,5926165880,658515091,1125114380,1436433218}
customsongtable = {}
game.ReplicatedStorage.ClientEvents.CustomSongEvent.OnServerEvent:Connect(function(player,idofsong)
	table.insert(customsongtable,idofsong)
end)

while true do
	if #customsongtable == 0 then
		local randosong = math.random(1,9)
		game.Workspace.MainSound.SoundId = "rbxassetid://" .. songtable[randosong]
		game.Workspace.MainSound.TimePosition = 0
		game.Workspace.MainSound.Playing = true
		game.Workspace.MainSound.Ended:Wait()
	else
		game.Workspace.MainSound.SoundId = "rbxassetid://" .. customsongtable[1]
		game.Workspace.MainSound.TimePosition = 0
		game.Workspace.MainSound.Playing = true
		table.remove(customsongtable[1])
		game.Workspace.MainSound.Ended:Wait()
	end
end

The the problem is even when the #customsongtable is empty, the random song loops only once then stops and doesn’t play any songs anymore.

And for custom songs when I try to insert a sound ID to the table the sound turns out to be a simple “-” even when I know it should come back as a song ID which I have put in through a text box from client.

Please correct me if I am doing something wrong anywhere.

Can anyone help?

(Character Limit)

Try this:

songtable = {7698151169,5140419602,465694735,859998992,402306149,5926165880,658515091,1125114380,1436433218}
customsongtable = {}
game.ReplicatedStorage.ClientEvents.CustomSongEvent.OnServerEvent:Connect(function(player,idofsong)
	table.insert(customsongtable,idofsong)
end)

while true do
	
	local songToPlay
	if #customsongtable == 0 then songToPlay=math.random(1,#songtable)
	 else songToPlay="rbxassetid://" .. customsongtable[1] end
	
	game.Workspace.MainSound.SoundId = "rbxassetid://" .. songtable[songToPlay]
	game.Workspace.MainSound:Play()
	game.Workspace.MainSound.Ended:Wait()
	
end

This code should fix the issue with the song only playing once. With your other problem regarding adding a songid to the table - we will need to see the code that sends the information to the server.

You’ll need to add table.remove(customsongtable, 1) after the RBXScriptSignal “.Ended” fires which means you’ll need a state variable/flag which represents whether or not the song played is a custom one or a default one.