Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my background music script only play 2 songs then stops?

As said in the title, my background music script which is supposed to play a random songs after one ends is only playing 2 songs before it stops.

Script:

local musicFolder = script.Parent.Music
local currentlyPlaying = script.CurrentlyPlaying

local firstSong = musicFolder:GetChildren()[math.random(1,#musicFolder:GetChildren())]
firstSong:Play()
currentlyPlaying.Value = firstSong.Name

musicFolder:WaitForChild(currentlyPlaying.Value).Ended:Connect(function()
	local nextSong = musicFolder:GetChildren()[math.random(1,#musicFolder:GetChildren())]
	nextSong:Play()
	currentlyPlaying.Value = nextSong.Name
end)

1 answer

✓ Accepted answer

I think your script would benefit from a loop. My advice is use this as a template and put the sound ids in the table at the top:

local Ids = {2530779901,1050582432}
local Sound = Instance.new("Sound", game.Players.LocalPlayer:WaitForChild("PlayerGui"))

while wait() do
	local Number = math.random(1,#Ids)
	local SoundId = "rbxassetid://" .. Ids[Number]

	Sound.SoundId = SoundId
	repeat wait() until Sound.IsLoaded == true
	Sound:Play()
	wait(Sound.TimeLength)
end

This has worked for me in the past so give it a go

This discussion is preserved as it was. New answers, comments, and votes are closed.