Music shuffling script playing multiple songs at once

I have a script in my game which plays random songs (shuffled) when a player joins, and cycles through them randomly. This script is client-sided so that it will start a new song when a player joins, just to make it more pleasing for the player. My problem is that quite frequently, when a player joins the game, it will play multiple songs at once, ranging from just 2 songs, to playing all of them at the same time. I have searched all over the devforum and have asked all of my experienced scripting friends and no one has been able to find a solution to this since the Roblox update which caused it a few months ago. Keep in mind I am not very experienced in scripting, I’m still learning but very slowly. I’m hoping that this is just a small issue that I somehow overlooked being as inexperienced as I am.

Here is the local script which is placed in StarterPlayerScripts:

local musicFolder = game.ReplicatedStorage.Music
local availableMusic = musicFolder:GetChildren()
local currentTrack = game.ReplicatedStorage.CurrentTrack

while true do
	
	wait(2)
	
	local randomTrack = availableMusic[math.random(1,#availableMusic)]
	
	currentTrack.Value = "..."
	
	wait (3)
	
	randomTrack:Play()
	
	currentTrack.Value = randomTrack.Name
	
	wait(randomTrack.TimeLength)
	
end

You should consider using Sound.Ended:Wait()

1 Like

which part should I replace with that?

it is Ended, you are right

the last line in the while loop you showed

1 Like

This worked, thank you so much!! Two months of searching for a solution just to get an answer within a minute asking on here, I really appreciate it!

I replaced it and it worked, thanks alot!

1 Like

api will be your best friend:

https://developer.roblox.com/en-us/api-reference/class/Sound

If you still can’t figure out something from api reference then dev forums

1 Like