Song Playlist

How To Create a Song Playlist That Everyone in Your Game can Listen To!

by lnsertYourself

Author Avatar

Est. read time: 10 minutes

Introduction

Thank you for clicking on my tutorial. Today, I will be instructing you on how to create an easily-adjustable song playlist script.

The type of playlist we will be making is a simple one. It starts at the first song, waits for it to finish, then plays the next song. And when the final song in the playlist finishes, the first song in the playlist will play again and will continue this loop forever. Although many playlists stop at the last song, we want our playlist to play continuously in our server.

img|80x45

Background

There are not many prerequisites needed to follow this tutorial. All you need to know is how to use basic tables: How to create a table and retrieve a table’s index value.

The Setup

We first want to insert a Sound object inside of workspace. You should also give a meaningful name to any object you will be using. Here, we will name our Sound object “PlaylistSound”.

IMPORTANT: Sounds inserted into parts will only play for the client based on the Sound’s EmitterSize and MaxDistance properties relative to the part's position. Sounds inserted inside the local client (such as the PlayerGui) will only play for the client. Because we want everyone in the server to all listen to the same playlist and the same song, we parent the part to workspace and it will replicate to all clients in the server.

The Scripting

Let’s start by inserting a Script inside of ServerScriptService. Give the script a meaningful name, like “PlayPlaylist”.

We start by referencing our Sound object from workspace:

local playlistSound = workspace:WaitForChild("PlaylistSound")

Then we want to create a table of songIDs we want to play. The order of the table will indicate the order that the playlist will play. I will also be adding comments to our table to show what song the songIDs represent. This is helpful incase you want to remove a song or want to add another song but make sure you’re not copying the same song.


local songs = {
    279206904, -- Alan Walker -  Fade 
    1088486530, -- Alan Walker -  Spectre 
    594599447, -- Cartoon -  On & On (feat Daniel Levi) 
    603074715 -- DEAF KEV -  Invincible 
}

Disclaimer: The songs I am using for this tutorial are made by NCS (NoCopyrightSounds) and will not infringe on Roblox's Copyright Policy. For any other sounds that you use, make sure it is also not copyrighted or you have permission to use it whether it be by written permission from the copyright creator or if it is protected under a royalty-free license and you have bought the rights to use it for your games.

Next, we create a variable for what index in our table we would like to start at. Since we want to start at the beginning, our index will be 1:

local index = 1

Now that we have everything setup, it’s time to start looping through our playlist! We start by setting up our while true do loop:

local index = 1
while true do

end

First thing’s first, we want to load in our first song into the Sound object we just referenced:

local index = 1
while true do
    playlistSound.SoundId = "rbxassetid://"..songs[index]
end

Now then, songs are not immediately available to be played when you change a Sound object’s SoundId. This is because the song needs time to load-in before we can play it. An analogy can be made with starting up your computer. You press the power button, but you can’t use it immediately. It needs time to load in all your files. The same goes for sounds. We wait for the song to load-in like so:

local index = 1
while true do
    playlistSound.SoundId = "rbxassetid://"..songs[index]
    repeat wait()

    until playlistSound.IsLoaded == true
end

So after much time testing this script initially, there was a bug where the songs would start at random time positions and change songs in quick succession. So after much of debugging, setting the Sound.TimePosition property to 0 before playing the song fixed this issue. So let’s do that here:

local index = 1
while true do
    playlistSound.SoundId = "rbxassetid://"..songs[index]
    repeat wait()

    until playlistSound.IsLoaded == true
    playlistSound.TimePosition = 0
end

Now that our sound is loaded and timed properly, it’s time to play it! And we will follow this up with an event that we would like to wait for to happen. More specifically, the Sound.Ended event. This event will fire when the Sound has finished playing.

local index = 1
while true do
    playlistSound.SoundId = "rbxassetid://"..songs[index]
    repeat wait()

    until playlistSound.IsLoaded == true
    playlistSound.TimePosition = 0
    playlistSound:Play()
    playlistSound.Ended:Wait()
end

Now after our song has ended, we want to move onto the next song to play. However, it’s not so straight-forward (or maybe it is depending on how you look at it). If we’re at the last SongID in our table, then the next index value will be nil! So we set a condition to go back to the first index of our songs table if we are currently at the last index of our table.

local index = 1
while true do
    playlistSound.SoundId = "rbxassetid://"..songs[index]
    repeat wait()

    until playlistSound.IsLoaded == true
    playlistSound.TimePosition = 0
    playlistSound:Play()
    playlistSound.Ended:Wait()
    if index == #songs then
        index = 1
    else
        index = index + 1
    end
end

The Final Script


local playlistSound = workspace:WaitForChild("PlaylistSound")
 -- Our sound object reference

local songs = {
    279206904, -- Alan Walker -  Fade 
    1088486530, -- Alan Walker -  Spectre 
    594599447, -- Cartoon -  On & On (feat Daniel Levi) 
    603074715 -- DEAF KEV -  Invincible 
}

local index = 1
 -- What index to start our playlist
while true do
 -- The start of our loop
    playlistSound.SoundId = "rbxassetid://"..songs[index]
 -- Retrieve the index value and place it into SoundId
    repeat wait()

    until playlistSound.IsLoaded == true
 -- Wait until our song is loaded in and ready to play
    playlistSound.TimePosition = 0
 -- Set initial time position of our song
    playlistSound:Play()
 -- Play the song
    playlistSound.Ended:Wait()
 -- Wait for song to end
    if index == #songs then
 -- Checks if we're at the last song
        index = 1
 -- Then go back to the first song
    else
        index = index + 1
 -- Else, go to the next song
    end
end
 -- And go back to the beginning of our loop

Time to Test

Before you ship your final product, you have to test it first! We will do this by inserting a Part with a ClickDetector and a Script. Inside the script we will reference the PlaylistSound and ClickDetector and when the ClickDetector is clicked, the sound’s TimePosition will be set to the Sound’s TimeLength (signifying that the song has ended). If everything is working properly, then the sound should play the next song and back to the beginning of the playlist when it is at the final song of our playlist. So inside the script, we would write something like this:

local playlistSound = workspace:WaitForChild("PlaylistSound")
local clickDetector = script.Parent:WaitForChild("ClickDetector")
clickDetector.MouseClick:Connect(function()
 -- When the part is clicked
    playlistSound.TimePosition = playlistSound.TimeLength
 -- End the song
end)

Conclusion

Music is great, but playlists are even better!

I hope you learned something from this tutorial, because I learned a lot making it. If this tutorial reaches above 4 stars with 20+ reviews, then I will make a part 2 medium tutorial of how to control your playlist such as pausing, playing, previous, next, repeat and shuffle!

🤗 I hope you enjoyed and have an awesome time scripting! 📜

View in-game to comment, award, and more!