Music plays all at the same time sometimes

Hey there, I have a problem where the music plays all at the same time sometimes and I’m not too sure why. It’s very rare but it happens when I join the game. Here’s the code:

local musicFolder = game.ReplicatedStorage.Music
local music = musicFolder:GetChildren()
local currentTrack = game.ReplicatedStorage.CurrentTrack
	
while true do
	local randomTrack = music[math.random(1, #music)]
	currentTrack.Value = "..."
	wait(2)
	
	randomTrack:Play()
	currentTrack.Value = currentTrack.Name
	wait(randomTrack.TimeLength)
end

use this:

randomTrack.Ended:Wait()

Do I just add that in or do I replace it with something?

Hello, I used your script in a baseplate and try to replicate the issue, but I wasn’t able to. I changed your script up a little bit so try this out and lmk if it works.

local musicFolder = game.Workspace.Music --The music folder (Changed to Workspace so the music would play
local music = musicFolder:GetChildren() -- A table of all the music
local currentTrack = game.ReplicatedStorage.CurrentTrack --Current track (string value

while true do
	local randomTrack = music[math.random(1, #music)] --Choosing a random track
	currentTrack.Value = "..." --Changing the value to "..." 
	wait(2) --Waiting 2 seconds

	randomTrack:Play() --Playing the randomly chosen sound
	currentTrack.Value = randomTrack.Name --Changing the CurrentTrack's value to the RandomTrack's name
	randomTrack.Ended:Wait() --Waiting until the sound ended.
end
3 Likes

Replace it with “wait(randomTrack.TimeLength)”

1 Like