Music playlist problem

Background: I have a game that teleports you between different places, the places have separate playlists of music I want to cycle through. When you teleport a remote event fires and this local sound script inside playergui receives it.

Problem 1: the functions are running in the background, is there a way to disconnect them right away?
Problem 2: when you teleport back to a place, it restarts the playlist back to the first song it played.
Problem 3: Once all the music of a playlist is finished playing, it doesnt loop and continue.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local event1 = ReplicatedStorage.FireSound

local pre = ""
local cur = ""
local isLocationChanged = false

local function playLocationPlaylist(loc)
	local playlist = SoundService:WaitForChild(loc)
	local breakOuterLoop = false
	
	for _, sound in pairs(playlist:GetChildren()) do
		print("start")
		if loc ~= cur then
			break
		end
		for _, g in pairs(playlist:GetChildren()) do
			if g.Playing then
				breakOuterLoop = true
				break
			end
		end
		if breakOuterLoop then
			break
		end
		sound:Resume()
		wait(sound.TimeLength-sound.TimePosition)
	end
end
local function onLocationChanged(loc)
	if cur == loc then
		print("same")
	else
		pre = cur
		cur = loc
		if SoundService:FindFirstChild(pre) then
			for _, su in pairs(SoundService:WaitForChild(pre):GetChildren()) do
				su:Pause()
			end
		end
		playLocationPlaylist(loc)
	end
end
event1.OnClientEvent:Connect(onLocationChanged)

onLocationChanged("Main")--music on join

Im stuck, and have no idea how to improve this code or if I’m doing it the right way. Please let me know how to fix and make this code run more efficiently. Thanks!

To disconnect events when teleporting, you can use the Disconnect method on the connection. However, in your case, you’re using OnClientEvent, which is a one-time connection. If you want to ensure it only runs once, you might consider using a boolean flag.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local event1 = ReplicatedStorage.FireSound

local pre = ""
local cur = ""
local isLocationChanged = false

local function playLocationPlaylist(loc)
	local playlist = SoundService:WaitForChild(loc)
	local breakOuterLoop = false
	
	for _, sound in pairs(playlist:GetChildren()) do
		print("start")
		if loc ~= cur then
			break
		end
		for _, g in pairs(playlist:GetChildren()) do
			if g.Playing then
				breakOuterLoop = true
				break
			end
		end
		if breakOuterLoop then
			break
		end
		sound:Resume()
		sound.Ended:Wait()  -- Wait for the sound to finish
		sound:Stop()  -- Stop the sound to reset it
	end
end

local function onLocationChanged(loc)
	if cur == loc then
		print("same")
	else
		pre = cur
		cur = loc
		if SoundService:FindFirstChild(pre) then
			for _, su in pairs(SoundService:WaitForChild(pre):GetChildren()) do
				su:Pause()
			end
		end
		playLocationPlaylist(loc)
	end
end

event1.OnClientEvent:Connect(onLocationChanged)

-- Set up a flag to ensure the event runs only once
local hasJoined = false

-- Function to run on join
local function onJoin()
	if not hasJoined then
		onLocationChanged("Main")
		hasJoined = true
	end
end

onJoin()