Music playlist just stop after cycling through all the music

This script I made, is used to cycle through a playlist of songs, but when it gets to song 4, it just stops and doesnt cycle through anymore.

My script -
local Sound1 = game.Workspace.Sounds.Sound1
local Sound2 = game.Workspace.Sounds.Sound2
local Sound3 = game.Workspace.Sounds.Sound3
local Sound4 = game.Workspace.Sounds.Sound4
while true do
Sound1:Play()
Sound1.Ended:Wait()
Sound2:Play()
Sound2.Ended:Wait()
Sound3:Play()
Sound3.Ended:Wait()
Sound4:Play()
wait(1)
end

Please use three backticks (```) before and after your code, and also indented neatly for readability. Here’s your code again but more readable.

local Sound1 = game.Workspace.Sounds.Sound1
local Sound2 = game.Workspace.Sounds.Sound2
local Sound3 = game.Workspace.Sounds.Sound3
local Sound4 = game.Workspace.Sounds.Sound4
while true do
  Sound1:Play()
  Sound1.Ended:Wait()
  Sound2:Play()
  Sound2.Ended:Wait()
  Sound3:Play()
  Sound3.Ended:Wait()
  Sound4:Play()
  wait(1)
end

Here are the changes I would make.

  1. Use an array to play sounds instead of just referencing them all. This means that you can add as many songs as you want later.
  2. Iterate through the array in a while true do loop.
  3. Use task.wait() instead of wait(1)

Here’s that in action:

local soundIds = {sound1, sound2, sound3, sound4}
local soundplayer = Instance.new("Sound")
while true do
	for i,v in soundIds do
		soundplayer.SoundId = v
		soundplayer:Play()
		soundPlayer.Ended:Wait()
	end
end

Be sure to replace soundIds with the sound IDs of your sounds.
Also, it’s better to have 1 sound object and change the soundId instead of having multiple sound objects as sound objects can be quite laggy.

ok thanks, but now nothing is playing at all

Errors? Did you use the sound objects instead of the soundIds?

no, I used the sound id list thing, playing off of a single sound object

I really new to scripting lol, so yea, some of this stuff in confusing

Also, yes, there is an error, the sound data not being able to download. So it works most likely, I just have to fix that error

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.