Why is this music system not working?

Hello! I am using this music system, which had an issue, which was resolved. But now I am still struggling to figure this out. I have spent countless hours trying to watch tutorials and using systems off #resources, but I can never get them to work. I need a system that everyone hears at a near simultaneous time. Here is the code that I have tried:

local music = game.SoundService.Music -- The path to the sound object you created in the SoundService. If that doesn't work, try replacing Soundscape with SoundService.
local songs = game.ServerStorage.Songs:GetChildren() -- The :GetChildren() method returns a lua table containing the objects inside the instance we used it on. In this case, that's the Songs folder we created in the ServerStorage.
local queue = {} -- Here, we will be putting all the songs in the songs folder and p

script.addSongs.Event:Connect(function()
	for _,song in pairs(songs) do -- This for loop will iterate over every object in our songs folder.
		if song:IsA("IntValue") then -- We check if the song is an IntValue to make sure it's a song.
			table.insert(queue, {
				["songName"] = song.Name, -- The name of the song. As we mentioned, it is the name of the IntValue.
				["songID"] = "rbxassetid://"..tostring(song.Value) -- The ID of the song. As we mentioned, it is the value of the IntValue. We add rbxassetid:// before it because sound objects have it in that format.
			}) -- This will insert a table inside our queue table containing the information for our song.
		end	
	end
	
	for i = 1, #queue - 1 do
		local r = math.random(i,#queue)
		queue[i], queue[r] = queue[r], queue[i]
	end -- This shuffles the table. This method of shuffling is called the "Fisher-Yates Shuffle". If you wish to find out how it works, go search it up!
end)

script.playSong.Event:Connect(function()
	if #queue == 0 then script.addSongs:Fire() end -- Here, we check if there are no more songs in the queue table. If there aren't, then we add them again.
	
	local songToPlay = queue[1] -- This is the next song in the queue table. Further into this function, we remove that from the table as it's already playing.
	music.SoundId = songToPlay.songID -- We set the sound ID property of the sound object to the songID we defined in the song to play.
	music:Play() -- This plays the song.
	
	table.remove(queue, 1) -- This removes the song from the table as it's already playing.
	
	music.Ended:Wait() -- We wait for the song to finish, and then we play the next one.
	
	local delayBeforeNextSongPlays = 3 -- The amount of time we will wait before the next song plays after the current one is done.
	
	wait(delayBeforeNextSongPlays)
	
	script.playSong:Fire() -- We run this function again.
end)


script.addSongs:Fire()-- Calls the addSongs() function which as we said, adds the songs into the queue table.
script.playSong:Fire() -- Plays the next song available in the queue. :) 

But this stops playing songs at random for some reason. Could someone help me make a new system, or fix the current one? Thank you so much! :slight_smile:

Try using

math.random

That might help you out. I didn’t fully read the script, so I apologize if that doesn’t help.

The script is using it, I believe, correct me if I’m wrong, I’m not a scripter.

Oh yeah, you’re right. My bad, sorry.

1 Like

I made a YouTube video showing how to create a music system from scratch:

I’ve tested it multiple times and it always works. Just be sure to follow the steps carefully. Hope I helped you out!

Do you know if this tutorial is server-sided so everyone here’s the music at the same time?

Even though it’s in a local script? I’m going to try it though, thanks.

1 Like

Did you make a changed to the original script, or did you keep it the same?

Did you make changes, you wrote it so it sounds like you edited the script from the video.

Nope, the script in the video works perfectly fine. I did not make any changes.

1 Like

Just tried the script, it repeats the same audio over and over, even though I have different ones. Here is the script, just like in the video:

local MusicFolder = game.ReplicatedStorage.Music

local AvailableMusic = MusicFolder:GetChildren()

local randomTrack = AvailableMusic[math.random(1,#AvailableMusic)]

while true do

wait(3)

randomTrack:Play()

wait(randomTrack.TimeLength)

end

What am I doing incorrectly?

Probably math.random is picking the same music in a row before it picks another song.

It’s done that 65 times in a row… there’s 4 other tracks.

Instead of using math.random, as you don’t want it to pick the same song again, I’d use for = 1, #AvailableMusic loop, so it’ll play that song, then get the song after it.

Could you send an example of what you mean? Not sure how to do what you just explained…

Like this?:

while true do
	local MusicFolder = game.ReplicatedStorage.Music
local AvailableMusic = MusicFolder:GetChildren()
local randomTrack = AvailableMusic[math.random(1,#AvailableMusic)]
	wait(3)
	randomTrack:Play()
	wait(randomTrack.TimeLength)
end
1 Like

That seemed to work, could you join me in game to test that everyone hears the same audio at a near same time?

https://www.roblox.com/games/5042839799/Chain-Aesthetic-Homestore?refPageId=670027a4-9b91-469b-9839-b380e0787b63

1 Like
local musicFolder = game.ReplicatedStorage:WaitForChild("Music"):GetChildren() -- getting the folder's sounds
local songObj = Instance.new("Sound", game.Workspace) -- creating a sound for everyone to hear
songObj.MaxDistance = "inf"
repeat wait()
for i = 1, #musicFolder do -- this will loop through the folder
    local nowPlaying = musicFolder[i]
    songObj.SoundId = nowPlaying.SoundId
    songObj:Play() 
end
until not songObj.IsPlaying and false
-- script in ServerScriptService (server-sided)

The only issue I would see with that is once the playlist is gone through completely, the music would stop. Other than that, that’s a better option.

It won’t stop, as I recently tried it with my 2 songs and it seems to loop once it has gone through the folder.