Music System Not Changing Song

Hello, recently I was just testing out my music system and it seemed to just keep playing the same song over and over again. Though everything works with queue, and playing and skipping songs!

Why is the song repeating its self?

Here is the code →

--[[
	@author TwinPlayzDev_YT
	@credit SlipperySpelunky/SaifLachmann (Dev Forum Helpers), Fexzi, C9 (Discord Helpers)
	@since 1/28/22
	This script basically controls the music player.
--]]

--[ SERVICES ]--

local Players = game:GetService("Players") -- The Player service.
local rep = game:GetService("ReplicatedStorage") -- The ReplicatedStorage service.
local MarketplaceService = game:GetService("MarketplaceService") -- The Marketplace service.

--[ MAIN LOCALS ]--

local CompletedBindable = Instance.new("BindableEvent")
CompletedBindable.Name = "CompletedBindable"
CompletedBindable.Parent = game:GetService("ServerStorage")

local events = rep.EVENTS.MusicEvents -- This variable is the folder in which the remote events/functions are stored in.

local queue = {}
local songs = {
	2759694106,
}

local currentSong = "No song playing"

local sound = game.Workspace:FindFirstChild("GlobalSound")

--[ GET PLAYING SONG FUNCTION ]--

events.GetPlayingSong.OnServerInvoke = function()
	return currentSong -- When the GetPlayingSong remote function gets invoked, we will return the currently playing song name back to the client who invoked it.
end

--[ SONG REQUEST FUNCTION ]--

events.RequestSong.OnServerEvent:Connect(function(player, id)
	print("REQUEST SONG WAS FIRED : " .. id)
	if id then
		local success, data = pcall(MarketplaceService.GetProductInfo, MarketplaceService, id)
		if success and data and data.AssetTypeId == 3 then
			table.insert(queue, id) -- inserting Id into queue table
			events.RefreshQueue:FireAllClients(queue) -- firing refresh queue for later
		end		
	end
end)

--[ MAIN PLAYER ]--

while true do
	if #songs > 0 then -- We first check if there are any songs implemented into the system. If so, we proceed.
		sound.TimePosition = 0 -- We set the TimePosition property of our sound object to 0. This is done so when a new song starts playing, it doesn't continue from the TimePosition of when the previous song ended at.
		local selectedSong -- We create our selectedSong variable. It will be properly declared in the following code.
		
		if #queue > 0 then -- We check if there are any requested songs. If so, we select the first song that was requested.
			selectedSong = queue[1] -- Sets the selected song variable as the requested song.
			table.remove(queue, 1) -- Removes the selected song from the queue table.
			events.RefreshQueue:FireAllClients(queue) -- We fire all clients with the RefreshQueue remote event to refresh the queue as it was modified.
		else
			selectedSong = songs[math.random(1, #songs)] -- If there are no requested songs, then we select a random song from our songs table.
		end
	
		sound.SoundId = "rbxassetid://"..tostring(selectedSong) -- We set the SoundId property of the sound object to our selected song's ID.
		
		local count = 0
		
		repeat
			wait(.1)
			count = count + .1
		until sound.IsLoaded or count >= 4

		if sound.IsLoaded then
			sound.PlaybackSpeed = 1 -- We set the PlaybackSpeed property of the sound to the Pitch index of our selected song. If there is no pitch index, then we set it to 1.
			sound:Play() -- We begin playing the song.
			events.NewSong:FireAllClients(selectedSong) -- We fire the NewSong event to all clients to indicate a new song has started playing.
			currentSong = selectedSong -- We set the currentSong variable to the name of the selected song.
			--repeat wait() until not sound.IsPlaying
			CompletedBindable.Event:Wait()
		end
		
	else -- if no songs playing we break out of the loop at 98 and change the text of the sign
		break 
	end
end

--if sound.TimePosition == sound.TimeLength or not sound.IsPlaying then
--	CompletedBindable:Fire()
--end

sound.Ended:Connect(function() CompletedBindable:Fire() end)
sound.Stopped:Connect(function() CompletedBindable:Fire() end)

Recently, I also added the bindable event to help fix the skipping problem, but now for some reason the selectedsong is the same song over and over again. I don’t think my script knows if the sound has ended or not?

Properties of sound →
image

What could be the issue?

Because you have its “Looped” property enabled.

1 Like

Turned it off, and it doesn’t change anything rather than just not playing the song again. I still am having issues with making the song change.

There… appears to only be one song in the actual table. I’m not sure what else to say. Also, you can use the Sound.Ended event to register when the song has ended.

Ah, nevermind. You’re using a remotevent to suggest songs to queue up.

Well yes, that was for example.

I even tested using sound.Ended:Connect(function() CompletedBindable:Fire() end)

Any way to help out?

Well, I can provide an example system on how you could loop a queue system. I don’t want to spoonfeed the entire queue system, which I’m sure you can figure out.

local Ids = {
	1,
	2,
	3,
}
local Queue = {

}
local Sound = script.Parent
local LastSong

local function Loop()
	local function PlaySong()
		local Song = Queue[math.random(1,#Queue)]
		Sound.SoundId = Song 
		Sound:Play()
		Sound.Ended:Wait()
		Queue[Song] = nil
		Loop()
	end
	if #Queue < 1 then
		for i, v in ipairs(Ids) do
			Queue[i] = v
			Ids[i] = nil
		end
		PlaySong()
	else
		PlaySong()
	end
end

Loop()

(i have not tested this system, if there are any errors let me know.)

1 Like

i had something simiilar but what i did was made the script repeat sound:play until it senses the sound is already playing

Could you show me an example by any chance? Still having troubles.

i really dont think it would work because my system works way more simpler than yours but what i did was to just keep attempting to play the song until the script senses that it was playing

repeat
sound:Play()
until sound.IsPlaying == true

but your problem is that it is repeating the same song, while mine was that it played one song then stopped completely so i don’t think that this would help you

1 Like

I could help you if your system is like mine, where its ONLY randomized songs, but just that, the user cannot decide the songs, which is what i think you are trying to do

Fixed - >

	sound.Ended:Connect(function() 
			CompletedBindable:Fire() 
		end)

		sound.Stopped:Connect(function()
			CompletedBindable:Fire() 
		end)

		CompletedBindable.Event:Wait()
		

Makes no sense why, but this works.