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