LoadMap.lua, how can I make the map loader and spawner more efficient and cleaner?

Greetings! I have a script that is in charge of loading maps, and spawning players where they need to be. This script started getting messy when I implemented MatchmakingService to it, as they do not have an option to use Roblox’s TeleportData, and they use their own.

If you can figure out anyway to make this script more efficient and cleaner, that would be appreciated!

Thanks in advance!

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local MapLoaded = false
local PlayerLoadingMap = false
local GameStarted = false

local function LoadKart(Player, Slot, MatchPlayer, Character)
	while true do
		local TheirJeep = ReplicatedStorage.KartParts.Bodies[Player:WaitForChild("EquippedKart"):GetAttribute("Body")]:Clone()
		if not MapLoaded then
			repeat game:GetService("RunService").Heartbeat:Wait() until MapLoaded
		end
		TheirJeep.Parent = game:GetService("Workspace").Players
		TheirJeep:PivotTo(game:GetService("Workspace"):WaitForChild("Map").KartSpawnAreas[Slot].CFrame)
		TheirJeep.Name = Slot

		if ServerStorage.MapInfo:GetAttribute("Multiplayer") then
			ReplicatedStorage.MultiplayerRaceReady.Event:Wait()
		end
		
		ReplicatedStorage.MultiplayerManager:SetAttribute("IsReady", true)
		
		task.wait(6)

		if not MatchPlayer then
			ReplicatedStorage.MapLoader.OnLoaded:FireClient(Player)
		end

		while true do
			task.wait()
			local Character = game:GetService("Workspace"):WaitForChild(Player.Name)
			TheirJeep.DriveSeat:Sit(Character.Humanoid)
			if Character.Humanoid:GetState() == Enum.HumanoidStateType.Seated then
				break
			else
				Character.Humanoid.Jump = true
			end
		end

		if not MatchPlayer then
			ReplicatedStorage.StartCountdown:FireClient(Player)
		end

		TheirJeep.ChildRemoved:Wait()

		if not MatchPlayer then
			ReplicatedStorage.ShowRespawning:FireClient(Player)
		end
		TheirJeep.Parent = nil
		return TheirJeep
	end
end

local MapTable = {}

local function LoadMap(Player, MapName, Gamemode, Metadata)
	if not MapLoaded then
		PlayerLoadingMap = true
		ServerStorage.MapInfo:SetAttribute("Gamemode", Gamemode)
		if Gamemode then
			print(Player.Name.. "is loading the map. ")
			local MapClone = ServerStorage.Maps[MapName]:Clone()
			MapClone.Parent = game:GetService("Workspace")
			ReplicatedStorage.MapLoader.SetPlayerMusic:FireAllClients(MapClone:GetAttribute("Music"))
			MapClone.Name = "Map"

			for Property, Value in pairs(ServerStorage.Lighting[MapName]:GetAttributes()) do
				game:GetService("Lighting")[Property] = Value
			end

			for _, LightingEffect in pairs(ServerStorage.Lighting[MapName]:GetChildren()) do
				local Effect = LightingEffect:Clone()
				Effect.Parent = game:GetService("Lighting")
			end

			MapLoaded = true
			--LoadKart(Player, Player.Team.Name, false)
			if Gamemode == "MatchRace" then
				local MatchKart = LoadKart(ReplicatedStorage.Player2, "Plr2", true)
				local Character = ServerStorage.Characters[Metadata.Character]:FindFirstChildOfClass("Model"):Clone()
				Character.Name = "Player2"
				Character.Parent = game:GetService("Workspace")
				MatchKart.DriveSeat:Sit(Character:FindFirstChildOfClass("Humanoid"))
			end
		end
	end
end

Players.PlayerAdded:Connect(function(Player)
	LoadKart(Player, Player.Team.Name, false)
end)

ReplicatedStorage.MapLoader.GetMap.OnServerEvent:Connect(LoadMap)

ReplicatedStorage.LoadMultiplayerMap.Event:Connect(function(MapName)
	if not MapLoaded then
		PlayerLoadingMap = true
		local MapClone = ServerStorage.Maps[MapName]:Clone()
		MapClone.Parent = game:GetService("Workspace")
		MapClone.Name = "Map"
		ReplicatedStorage.MapLoader.SetPlayerMusic:FireAllClients(MapClone:GetAttribute("Music"))
		for Property, Value in pairs(ServerStorage.Lighting[MapName]:GetAttributes()) do
			game:GetService("Lighting")[Property] = Value
		end

		for _, LightingEffect in pairs(ServerStorage.Lighting[MapName]:GetChildren()) do
			local Effect = LightingEffect:Clone()
			Effect.Parent = game:GetService("Lighting")
		end
		MapLoaded = true
		game:GetService("Players").PlayerAdded:Connect(function(Player)
			ReplicatedStorage.MapLoader.SetPlayerMusic:FireClient(Player, MapClone:GetAttribute("Music"))
		end)
	end
end)