Music Queueing System

Hi there! I have tried making a music queueing system that includes requesting in it, but it doesn’t seem to work in some ways. One is that it’s really confusing for me and two, there’s some things I can’t figure out.

The primary functionality works, but sometimes it breaks. :frowning:

The way it works is that it gets all the songs in a certain folder with a for loop, and puts them in a table of the ID it has. It will then play the first song in that table. Then, by using the .Ended event of a sound object, I can easily detect when a song ends and play the next song in that queue. Logically, since it’s always removing the first object in that table, the next song is always gonna be the first index of it. But, this works differently if a user wants to request a song as multiple songs can be requested at once and inserting the song they requested as the first index will make it go in backward order.

I have also tried creating a separate table for the personal queue and checking if an object exists in that table when a song ends, but it feels redundant to me and that there’s a better way to do this.

This is my current code. I’m storing the songs in the client, even though I know I should store it in the server.

game.ReplicatedStorage.PersonalMusicQueue.OnClientEvent:Connect(function(songID, songName)
	local queueLabel = script.Parent.Parent.QueueList.QueueFrame.ExampleSong:Clone()
	queueLabel.Name = "Song"
	queueLabel.SoundID.Value = songID
	queueLabel.Text = songName
	queueLabel.Visible = true
	queueLabel.Parent = script.Parent.Parent.QueueList.QueueFrame
	table.insert(queueTable, 1, songID)
end)```
1 Like

I’m not trying to fix an error. I simply want an efficient way of making a Music Queueing System.

For short, to not fully answer your needs because asking for scripts isn’t allowed.

local Table = {
  "rbxassetid://ID1", "rbxassetid://ID2"
}
while true do
  if Table[1] ~= nil then
    script.Parent.SoundId = Table[1] -- play ID1 then ID2
    script.Parent:Play()
    script.Parent.Ended:Wait() -- waits for finish
    table.remove(Table, 1) -- removes from queue
  end
  wait(1) -- dont exhaust loop
end -- loops

-- Queue music (orderly) with
table.insert(Table, "rbxassetid://ID3")

Yes I already know that sir. As I said, I want to add requests to my current code. I don’t want to make a new one.

A better place for this might be in Game Design Support, then :slight_smile:

And you should still post code, it’s helpful. Also, what do you mean “it sometimes breaks”?

For my system it would be relatively easy.

table.insert(Table, "rbxassetid://ID3")
1 Like

But how would you play the song requested after the current song playing ends? I don’t want to add it all the way to the end of the queue.

Basically, things like wrong queue order. Of course, there wouldn’t be any errors in the console for this, so it’s really confusing for me.

With the Play function?

Also wait…

These contradict. You don’t want to remake it but want a whole script with no good context?

Ok let me re-phrase that second reply. I simply want an efficient way of REQUESTING songs for my music queueing system.

And also, I know how to play songs. I meant how would you get the current song playing and just add on the requested song to that queue after that current song?

Edited my first response. This should literally answer all of your questions. Anything beyond that is simply against this categories rules. You cannot ask for stuff in this category, that’s not what this category is for. I’m not helping beyond this since this is a request instead of a “help I can’t find the issue, here’s what I’m doing and this is what I tried”, which is how it should be used.

Ok then let me make this technical. What if I want to fix my CURRENT code to make it more efficient for the requesting songs part. Basically, I’m asking for help on how I would be able to fix my current requesting songs code to make it work like it’s supposed to. I’ll edit my post to include the code in it.

1 Like

There I edited my post with my current personal queue code.

You’re inserting to the index 1. This will move everything that should play before it above, prioritizing that song.

table.insert(queueTable, next, songID)

or simply just

table.insert(queueTable, songID)

Unless if you’re receiving the songs by the greatest Index (which is not ideal to begin with) I don’t see why you’re doing that.

Read the long part of my post. (By the way, I have two things. The requesting songs and the pre-defined songs. I want to play a requested song right after a pre-defined song has ended instead of putting it all the way at the end of the queue.)

local PredefinedSongs = {"song1", "song2", "song3"}
local SongQueue = {}

coroutine.wrap(function()
  while wait() do
    local Song
    if not #SongQueue > 0 then
      Song = tostring(PredefinedSongs[1])
      table.remove(PredefinedSongs, 1)
      table.insert(PredefinedSongs, Song)
    else
      Song = tostring(SongQueue[1])
      table.remove(SongQueue, 1)
    end
    -- you did not define where exactly the script is located, so im going to act like script is parented to the song player.
    script.Parent.SoundId = Song
    script.Parent:Play()
    script.Parent.Ended:Wait()
  end
end)()

game:GetService("ReplicatedStorage"):WaitForChild("PersonalMusicQueue").OnClientEvent:Connect(function(SongID, SongName)
  local QueueLabel = script.Parent.Parent.QueueList.QueueFrame.ExampleSong:Clone()
  QueueLabel.Name, QueueLabel.SoundID.Value, QueueLabel.Text, QueueLabel.Visible, QueueLabel.Parent = "Song", SongID, SongName, true, script.Parent.Parent.QueueList.QueueFrame
  table.insert(SongQueue, SongID)
end)

Don’t mind formatting or if there’s some typos, I wrote this on mobile.

Sorry if I’m being too strict, but I already said I want a requested song to play right after a pre-defined song has ended. I don’t want to put a requested song as the last index of the queue table.

A good music queueing system would be having a server script loop through a table, Remove the songs it has played from the table until it is empty and then have it add all the songs back into the table so it will never end.

You can also just have the Server run the sounds itself.

A good feature I like to have is fade my old sound out while having the new one fade in and removing the older sound. :wink:

Yes, but I can’t figure out of how I would get the NEXT requested song and play it after a pre-defined song has ended. I also can’t figure out how to actually order the requested songs, as well I want them to play after a pre-defined song has ended, not add it as the last index in the table.

It you want to order them then just insert them with some sort of order definition.
As for playing the next song, Im pretty sure a while true do loop would serve just as fine.