Sound System Glitch

Hello! I am currently working on a sound/queue system. For the past hour, I’ve been trying to debug this script, and I finally found the issue. The :Play() function is not playing the sound. I checked, and the sound ID loads properly. I then used a print(sound.IsPlaying) function to see if it was playing. This function returned true. The sound does not play in the game. I’m not sure if this has something to do with the server/client, if there’s an issue with my code, or if it’s an engine bug. I may just need another pair of eyes to look at it.

local repStorage = game:GetService("ReplicatedStorage")
local songPlayEvent = repStorage:WaitForChild("SongPlayEvent")
local queue = {}

local function makeSound()
	local new = Instance.new("Sound", game:GetService("SoundService"))
	new.Name = "SoundSystem"
	return new
end

local function changeBanner(song, artist)
	game:GetService("StarterGui").SongArtist.TopBanner.Text = song.." - "..artist
end

local sound = makeSound()
local value = false

local function playSong()
	while true do
		if not value then
			for i,v in pairs(queue) do
				local split = v:split("-")
				local productInfo = game:GetService("MarketplaceService"):GetProductInfo(tonumber(split[2]))
				if productInfo.AssetTypeId == 3 then
					sound.SoundId = "rbxassetid://"..productInfo.AssetId
					sound.Loaded:Connect(function()
						sound:Play()
						value = true
						changeBanner(split[1], split[2])
						sound.Ended:Connect(function()
							value = false
							table.remove(queue, 1)
							sound:Pause()
						end)
					end)
				end
			end
		end
	wait()
	end
end

coroutine.resume(coroutine.create(playSong))

local purchases = game:GetService("DataStoreService"):GetDataStore("Purchases")

game:GetService("MarketplaceService").ProcessReceipt = function(receipt)
	local id = receipt.PlayerId.."-"..receipt.PurchaseId
	local success = nil

	pcall(function()
		success = purchases:GetAsync(id)
	end)

	if success then
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	local player = game.Players:GetPlayerByUserId(receipt.PlayerId)

	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	else
		if receipt.ProductId == 1112783830 then
			player.PurchasedProduct.Value = true
		end

		pcall(function()
			purchases:SetAsync(id, true)
		end)
		return Enum.ProductPurchaseDecision.PurchaseGranted

	end
end

songPlayEvent.OnServerEvent:Connect(function(plr, id)
	local isAllowedPlay = plr.CanPlay.Value
	local isOnCooldown = plr.OnCooldown.Value
	local purchasedProduct = plr.PurchasedProduct.Value
	local function add()
		local queuePos = #queue+1
		local queueId = plr.Name.."-"..id
		table.insert(queue, #queue+1, queueId)
		print(queue[queuePos])
		print(queueId)
		print(queue[queuePos] == queueId)
		if queue[queuePos] == queueId then
			return true
		else
			return false
		end
	end
	if isAllowedPlay then
		if isOnCooldown then
			return
		else
			local addFunction = add()
			if addFunction then
				isOnCooldown = true
				wait(120)
				isOnCooldown = false
			end
		end
	end
	if purchasedProduct then
		local addFunction = add()
		if addFunction then
			plr.PurchasedProduct.Value = false
		end
	else
		game:GetService("MarketplaceService"):PromptProductPurchase(plr, 1112783830)
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	local isAllowedSkip = Instance.new("BoolValue", plr)
	isAllowedSkip.Value = false
	isAllowedSkip.Name = "CanSkip"
	local isAllowedPlay = Instance.new("BoolValue", plr)
	isAllowedPlay.Value = false
	isAllowedPlay.Name = "CanPlay"
	local isOnCooldown = Instance.new("BoolValue", plr)
	isOnCooldown.Name = "OnCooldown"
	isOnCooldown.Value = false
	local purchasedProduct = Instance.new("BoolValue", plr)
	purchasedProduct.Value = false
	purchasedProduct.Name = "PurchasedProduct"
	if plr:GetRankInGroup(3743720) >= 12 then
		isAllowedSkip = true
	end
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 12541323) then
		isAllowedPlay = true
	end
	plr.Chatted:Connect(function(chat)
		if string.lower(chat) == ":skip" then
			if isAllowedSkip then
				table.remove(queue, 1)
				sound:Pause()
				value = true
			end
		end
	end)
end)
2 Likes

Try changing the parent of the sound from SoundService to Workspace

Edit: Also, I don’t recommend using the parent argument for Instance.new