Background song keeps restarting randomly

--[[

-- DonaldDuck5150 --

]]

local List = {"7023741506", "7023680426", "7029005367", "7028932563", "5409360995", "7023690024", "7029017448", "7029024726"}

local Sound = game.StarterGui.Sounds.BackgroundMusic

while true do 
	
	for i = 1, #List do
		
		Sound.SoundId = "rbxassetid://"..List[i]
		
		task.wait(5)
		
		Sound:Play()
		
		task.wait(Sound.TimeLength)
		
	end
end

This is my sound system that I use. It works fine but the song will keep randomly restarting for no reason and I don’t know why. It won’t even be 10 to 20 seconds into the first song and it just randomly starts over.

This is a LocalScript in StarterGui and my sound that I’m using is also in the StarterGui inside of a folder containing my other various sounds.

Any idea why it’s doing this? Is there a better way to write this script or is there a better place to put it?

You should probably look into sound.ended it might help. I dont know why its restarting tho

I’ve found that whenever I reset my character it starts over again. Anyone know how to fix my code so that it doesn’t restart when I respawn?

So, its a simple fix. Put the audio inside SoundService and play the audio through a server script in ServerScriptService

local audio = game:GetService("SoundService"):WaitForChild("audio.Name") -- :WaitForChild so it can load in and find the audio!
if audio then --Checks if the server found the audio or not!
audio:Play() -- Plays the audio
end

Edit 1: It will play everyone

It seems as if this is in a ScreenGui. Modify the properties of the ScreenGui so that ResetOnSpawn is set to false.

It’s not in a StarterGui but you are still right because if it was in a ScreenGui then that property would need to be set to false

I’ve found the solution. All you need to do is put it in StarterPlayerScripts rather than StarterGui because the StarterGui resets every time the player resets. Thank you everyone for pitching in!

No problem Dev Forum is super helpful!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.