Why are multiple songs playing?

Hey guys.

So I have this music script that will play songs held in replicated storage that works pretty well. However it occasionally starts playing 2-3 songs when a player joins instead of just one.

Is there something I can add to ensure that it only ever plays one song??

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

wait()

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

You can call the :Stop() method of the Sound before starting the next one. Maybe rearrange your code like:

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

while true do
	
   if randomTrack then
      randomTrack:Stop()
   end

   randomTrack = availableMusic[math.random(1, #availableMusic)]
	
   currentTrack.Value = "..." -- nothing is playing yet
	
   wait(2)
	
   randomTrack:Play()

   currentTrack.Value = randomTrack.Name
	
   wait(randomTrack.TimeLength)
	
end
1 Like

Hey there! I believe your song is on loop, that makes the sound play over and over again.
If that wasn’t the problem i would recommend using this script (if you wish).

(It comes with an optional mute button.)

local curmusic
local musicfiles = script.GameMusic --the music files
local mutebutton = script.Parent --the button used to umte
local initialvolume = 0.5
--delete from here if you don't want a mute button
mutebutton.MouseButton1Click:Connect(function()
	if script.Parent.ImageTransparency == 1 then
		script.Parent.ImageTransparency = 0
		curmusic.Volume = initialvolume
	else
		script.Parent.ImageTransparency = 1
		curmusic.Volume = 0
	end
end)
--delete till here if you don't want a mute button.
while true do
	local music = musicfiles:GetChildren()
	for count = 1,#music do
		local ran = math.random(1,#music)
		music[ran]:Play()
		curmusic = music[ran]
		music[ran].Ended:Wait()
		table.remove(music,ran)
	end
end

Thank you! So far this suggestion is working, but it’s a random issue so I’m waiting to see if it happens again.

I also wondered if it had something to do with @PhAnToMjose4605 solution. Some of your Sound objects may have their Looped property set to true when they shouldn’t be for your use case. Also, if you really wanted to be certain that only a single song is playing at any given time, you could loop through all the songs and call the :Stop() method on all of them before calling :Play() on the next one.

Well none of the songs are set to playing or looped. There is a playlist of songs in replicated that the script goes through, playing them one by one. When there are multiple songs that shouldn’t be playing, it goes back to only playing one song once the extra songs end.

Time length is not working idk why

Better use randomTrack.Stopped:Wait()

1 Like