Need help With Database and team saving

Does anyone know how to make it that the XP Give works separated for Each team and Also Saves the XP and rank for each team

it’s I don’t know how to make it

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local xp = leaderstats:WaitForChild("XPT")
local level = leaderstats:WaitForChild("RankT")

local xpData = {
	Team1 = 0,
	Team2 = 0,
	Team3 = 0,
	Team4 = 0
}

local xpPerMinute = 500

local rankCooldown = 0
local xpCooldown = 5

local function checkAndUpdateRank()
	while wait(rankCooldown) do
		local teamColor = player.TeamColor

		if teamColor == BrickColor.new("Really red") then
			if xpData.Team1 >= 500 and level.Value == "Loading" then
				level.Value = "O5-1"
			elseif xpData.Team1 >= 1500 and level.Value == "O5-1" then
				level.Value = "O5-2"
			elseif xpData.Team1 >= 4000 and level.Value == "O5-2" then
				level.Value = "O5-3"
			elseif xpData.Team1 >= 12000 and level.Value == "O5-3" then
				level.Value = "O5-4"
			end
		elseif teamColor == BrickColor.new("Forest green") then
			if xpData.Team2 >= 500 and level.Value == "Loading" then
				level.Value = "SID-1"
			end
		elseif teamColor == BrickColor.new("CGA brown") then
			if xpData.Team3 >= 500 and level.Value == "Loading" then
				level.Value = "CD-1"
			end
		elseif teamColor == BrickColor.new("Really black") then
			if xpData.Team4 >= 500 and level.Value == "Loading" then
				level.Value = "L-1"
			end
		end
	end
end

local function checkAndUpdateXP()
	while wait(xpCooldown) do
		local teamColor = player.TeamColor

		if teamColor == BrickColor.new("Really red") then
			xpData.Team1 = math.min(xpData.Team1 + xpPerMinute, 12000)
		elseif teamColor == BrickColor.new("Forest green") then
			xpData.Team2 = math.min(xpData.Team2 + xpPerMinute, 12000)
		elseif teamColor == BrickColor.new("CGA brown") then
			xpData.Team3 = math.min(xpData.Team3 + xpPerMinute, 12000)
		elseif teamColor == BrickColor.new("Really black") then
			xpData.Team4 = math.min(xpData.Team4 + xpPerMinute, 12000)
		end

		xp.Value = xpData.Team1 + xpData.Team2 + xpData.Team3 + xpData.Team4
	end
end

spawn(checkAndUpdateRank)
spawn(checkAndUpdateXP)