Trying to fix bug

You can write your topic however you want, but you need to answer these questions:

  1. Want to fix a bug, with my music player Keep it simple and clear!

  2. What happens is songs do not play after the first one, and song requests do not work. Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Tried adding waiting for loading, that didn’t work, looked for solutions elsewhere on the internet, that didn’t work, here’s my code, please help.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local soundtable = {5160230627,4734983736}
local songrequesttable = {}
local sound = Instance.new("Sound",game.Workspace)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local SongChange = ReplicatedStorage.SongChange
sound.Looped = false
while #songrequesttable == 0 do
for i,v in pairs(soundtable) do
		sound.SoundId = "rbxassetid://" .. v
	if not sound.IsLoaded then sound.Loaded:Wait() end
	SongChange:FireAllClients(v)
	sound:Play()
		sound.Ended:Wait()
		table.remove(soundtable,i)
	print("Song finished, swapping to next")
	end
end
while #songrequesttable >= 1 do
	for i,v in pairs(songrequesttable) do
	sound.SoundId = "rbxassetid://" .. v
	if not sound.IsLoaded then sound.Loaded:Wait() end
	SongChange:FireAllClients(v)
	sound:Play()
	sound.Ended:Wait()
	table.remove(songrequesttable,i)
	print("Song finished, swapping to next")
end
end
ReplicatedStorage.SongRequest.OnServerEvent:Connect(function(id)
	--add a prompt to make them buy a dev product--
	table.insert(songrequesttable,#songrequesttable + 1,id)
	print("test")
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

The while loops in your code caused ambiguities and just weren’t structured correctly, I restructured your code and I’ll explain what changes I made and how everything works. This is the final code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")

local SongRequest = ReplicatedStorage.SongRequest
local SongChange = ReplicatedStorage.SongChange
local SoundSource = Instance.new("Sound",game.Workspace)
SoundSource.Looped = false

local RBXAssetUrl = "rbxassetid://"
local DefaultIds = {5160230627,4734983736}
local RequestedIds = {}
local CurrentDefaultSound = 1

local function PlaySound(TargetId, Requested)
	SoundSource.SoundId = RBXAssetUrl..TargetId
	if not SoundSource.IsLoaded then SoundSource.Loaded:Wait() end
	SongChange:FireAllClients(TargetId)
	SoundSource:Play()
	if Requested then
		print("Playing Requested "..TargetId)
		RequestedIds[1] = nil
	end
	print("Playing "..TargetId)
end

local function OnSoundEnded()
	print("Current Song Ended")
	if #RequestedIds > 0 then
		print("Found Song Request")
		PlaySound(RequestedIds[1], true)
		return
	end
	
	if CurrentDefaultSound == #DefaultIds then
		CurrentDefaultSound = 1
	else
		CurrentDefaultSound = CurrentDefaultSound + 1
	end
	PlaySound(DefaultIds[CurrentDefaultSound])
end

local function OnSoundRequest(TargetId)
	print("Got Request "..TargetId)
	table.insert(RequestedIds, TargetId)
end

SoundSource.Ended:Connect(OnSoundEnded)
SongRequest.OnServerEvent:Connect(OnSoundRequest)

PlaySound(DefaultIds[CurrentDefaultSound])

Song Loop

As much as possible, try avoiding infinite loops made with while true do, I always look for ways to create a loop via events because I find them more efficient.

local function OnSoundEnded()
	print("Current Song Ended")
	if #RequestedIds > 0 then --checks if there are any requested songs
		print("Found Song Request")
		PlaySound(RequestedIds[1], true) --plays the song
		return
	end
	
	if CurrentDefaultSound == #DefaultIds then --checks if all the songs in the default sound list have been played
		CurrentDefaultSound = 1 --goes back to the first song
	else
		CurrentDefaultSound = CurrentDefaultSound + 1 --otherwise goes to the next song
	end
	PlaySound(DefaultIds[CurrentDefaultSound]) --plays the next default song
end

SoundSource.Ended:Connect(OnSoundEnded) --Fires when the current song is done

Song Request

Pretty much the code you made, but it didnt work because of the while loops you made which prevented the code from being reached. The only change I made here was the table.insert

local function OnSoundRequest(TargetId)
	print("Got Request "..TargetId)
	table.insert(RequestedIds, TargetId) --will automatically sort the songs as they are requested chronologically
end

SongRequest.OnServerEvent:Connect(OnSoundRequest)

Song Player

Pretty much your code again, but I made some changes just to identify whether the song was requested or not.

local function PlaySound(TargetId, Requested)
	SoundSource.SoundId = RBXAssetUrl..TargetId
	if not SoundSource.IsLoaded then SoundSource.Loaded:Wait() end
	SongChange:FireAllClients(TargetId)
	SoundSource:Play()
	if Requested then --checks if the song being played was requested
		print("Playing Requested "..TargetId)
		RequestedIds[1] = nil --removes the requested song from the queue
	end
	print("Playing "..TargetId)
end

I did my best to explain, but if you have any questions about how any of this works or if something isn’t working, feel free to reply or message me.

4 Likes

Nevermind, I used your code, and just changed a bit to use Song.Value.