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!
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
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.
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
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)