Issue with background music player

For my game, I’m using a script located in the workspace to play background music in the game. I have several instances

Issue: The script works, but sometimes after each song is done, there is a long pause before the next song plays.

I have checked all audio and none are broken and I’ve tried rewriting this is different ways but they all have this same problem.

script:

while true do

local Sounds = script:GetChildren()

local RandomIndex = math.random(1,#Sounds)

local RandomSound = Sounds[RandomIndex]

RandomSound:Play()

RandomSound.Ended:Wait()

end

To begin, you should define Sounds outside of the while true do. Second, instead of RandomSound.Ended:Wait() try repeat wait() until RandomSound.IsPlaying = false

Basically, this script:

local Sounds = script:GetChildren()
while true do
  local randomIndex = math.random(1,#Sounds)
  local randomSound = Sounds[RandomIndex]
  RandomSound:Play()
  repeat wait() until RandomSound.IsPlaying = false
end

This script should work fine as long as you don’t ever have the sound pause.

1 Like