Help with repeating music

Recently I started making a game and I want to have music in it. I am a new scripter so I don’t know much about making loops of different music. Could anyone tell me how to script this?

Thanks for reading. :slight_smile:

You can store all of your types of music inside an array. Then you can iterate over the array, pick a random song, play the song, then wait for that played song to finish.

Here is some example code:

local sound = ...

local songs = {
    1234567,
    908526,
    981762591,
    1566256
}

while true do
    local randomSong = songs[math.random(1, #songs)]
    
    sound.SoundId = "rbxassetid://" .. randomSong
    sound:Play()
    sound.Ended:Wait()
end

Play sound.
wait(secs) for duration of sound with additional gap if you want, playing a sound does not yield the thread.
Play sound again.

while wait(time) do
    SoundObject:Play()
end

Shortest way I can think of.

Regarding ur post:

  SoundObject.Looped = true;
  SoundObject:Play();

That’s a bit shorter and more accurate…

I believe the OP wants random music to play so that wouldn’t work either. @vtaetwrjxvdpgwjqkfzw, has the correct solution I believe.

Actually OP never stated that he wants random music, so:

local Sound = ...
local SoundList = {123456, 1234567} -- Sound Id's go here

while true do
  for _, soundId in pairs(SoundList) do
    Sound.SoundId = ("rbxassetid://%d"):format(soundId)
    Sound:Play()
    Sound.Stopped:Wait()
  end
end