How to make the music repeat once the last song is completed

Hello there. I’m Schedency, and I need help with a music script I have made.
The script is currently under the StarterGui, local script. The music is in a folder in the Workspace.

But, how do I make so it repeats every time? Instead of leaving the server, I want it to repeat from the beginning.
Is it any way to do that?

Here is my current script:

-- Varibles --
local sound1 = game.Workspace.Music.Sound1
local sound2 = game.Workspace.Music.Sound2
local sound3 = game.Workspace.Music.Sound3
local sound4 = game.Workspace.Music.Sound4
local sound5 = game.Workspace.Music.Sound5
local sound6 = game.Workspace.Music.Sound6
local sound7 = game.Workspace.Music.Sound7
local sound8 = game.Workspace.Music.Sound8
 
-- Script --
sound1:Play()
wait(33) -- Duration of the audio.
sound2:Play()
wait(252)
sound3:Play()
wait(144)
sound4:Play()
wait(186)
sound5:Play()
wait(178)
sound6:Play()
wait(189)
sound7:Play()
wait(150)
sound8:Play()
wait(200)

Thanks,
Schedency

1 Like

Use a loop, and instead of hardcoding the wait time why not use the sound.Ended event.

while true do
    for i = 1, 8 do
        local current_sound = game.Workspace["Sound" .. i]
        current_sound:Play()
        current_sound.Ended:Wait()
    end
end

What this will do is play all 8 sounds, waiting for them to finish to advance to the next sound.

5 Likes

Thanks, but do I need to add the loop for every music, or can I just copy and paste the line under for i, = 1, 8 do event?

I do not know what you mean. If you want to add more songs you just change the 8 to however many songs there are now. But for the sake of organization, I would put the sounds in a folder named Sounds in workspace. Then you can do game.Workspace.Sounds["Sound" .. i].

This would make the code slightly better:

local sounds = game.Workspace.Sounds:GetChildren()

while true do
    for idx = 1, #sounds do
        local current_sound = sounds[idx]
        current_sound:Play()
        current_sound.Ended:Wait()
    end
end
6 Likes

Thank you very much for your help!

Hi @Schedency, you’ll want to use a loop for that.

If you want these songs to be played indefinitely then I would recommend using a while loop, you can read up on how loops work here.

I would also utilise events to trigger the next song to play, you can read about events here.

As it sounds like you want all the music to be local, I would recommend just parenting the sounds to the script itself, and putting the script into “StarterPlayer → StarterPlayerScripts”. This way when your player dies, there is no risk of multiple songs starting to play one over the other, and instead the music would just continue to play without any issues upon dying.

Here is an example script that you could use once the sounds are parented under the script:

while true do
	local notUsed = {script.Sound1, script.Sound2, script.Sound3, script.Sound4, script.Sound5, script.Sound6, script.Sound7, script.Sound8}
	for i = 1, #notUsed do
		local ranNum = math.random(1, #notUsed)
		local songToPlay = notUsed[ranNum]
		songToPlay:Play()
		songToPlay.Ended:Wait()
		table.remove(notUsed, ranNum)
	end
end

This script will pick a random song that has not been played, and will then play it. Once all 8 songs have been played it will start again.

I have not tested this script so you may need to do some troubleshooting. :slight_smile:

EDIT: Fixed the script

5 Likes