Audio ID system not playing correctly? [Help]

Struggling with audio system, currently as it is when you join the game there are default songs already playing, when nothing is in the queue it will play one of these default songs, the problem i’m facing is sometimes these both randomly just do not play, they refuse to, I’ve tried three different solutions by rescripting how the sounds play and nothing works.

I’ve provided a video and some code

local function playDefaultSongs()
	local defaultList = musicList.DefaultSongs
	local index = math.random(1, #defaultList)
	
	local function playNextSong()
		if index > #defaultList then
			index = 1
		end
		
		local currentID = defaultList[index]
		index += 1
		
		local success, productInfo = pcall(function()
			return MarketplaceService:GetProductInfo(currentID)
		end)

		if success and productInfo then
			if productInfo.AssetTypeId == 3 then
				playSong:FireAllClients(productInfo)
			end
		end
		
		defaultSound.SoundId = "rbxassetid://"..currentID
		defaultSound.Loaded:Wait()
		defaultSound:Play()
		
		defaultSound.Ended:Connect(function()
			playNextSong()
		end)
	end
	playNextSong()
end
playDefaultSongs()

local function playSongsInQueue()
	if #queue == 0 then
		print("Queue is 0")
		isSongPlaying = false
		playDefaultSongs()
		return
	end
	
	if defaultSound.Playing then
		defaultSound:Stop()
	end
	
	isSongPlaying = true
	
	local currentID = table.remove(queue, 1)
	local productInfo = productInfoTable[currentID]
	productInfoTable[currentID] = nil
	
	playSong:FireAllClients(productInfo)
	
	currentSound.SoundId = "rbxassetid://"..currentID
	currentSound.Loaded:Wait()
	currentSound:Play()
	
	currentSound.Ended:Connect(function()
		isSongPlaying = false
		playSongsInQueue()
	end)
end

When you switch songs make sure the sound objects been fully reset and set back to 0

1 Like

imo its hard to help in this situation with only a part of the code, but perhaps something to do with the fact that ur connecting a new event each time a new song runs, hence it running the code multiple times? You should use :Once() instead of :Connect() at both places

Even if that isn’t your issue in this case, it’s still a memory leak, and should be fixed.

2 Likes

That wasn’t the fix exactly but yeah, in this case the script I provided is most of it, and I do believe its somewhere in there somehow I guess?

All my client script is doing is receiving the product data and applying it to the UI’s, figured it would just be easier to play the songs on the server

I suppose i’ll provide the last part of the script just incase, I really don’t know whats up with it though i’ve been searching through this all day

local function receiveID(player, ID)
	local success, productInfo = pcall(function()
		return MarketplaceService:GetProductInfo(ID)
	end)

	if success and productInfo then
		if productInfo.AssetTypeId == 3 then
			if not table.find(queue, ID) then
				table.insert(queue, ID)
				productInfoTable[ID] = productInfo
				print("ID added to queue", queue)
				if not isSongPlaying then
					playSongsInQueue()
				end
			else
				print("ID already in queue", queue)
			end
		end
	else
		print("Invalid musicID")
	end
end
musicID.OnServerEvent:Connect(receiveID)

Are you using one Sound instance for it, or recreating it every time the sound changes?

1 Like

I’m using a singular sound instance and changing its ID once it finishes

How about making a new sound instance every time a song starts, and deleting it every time it finishes? (still with :Once() connection)

It does seem to stop still, however it does print “playing a song”, but nothing plays