-- Local Functions

local function OnPlayerAdded(player)
	-- Setup leaderboard stats
	local leaderstats = Instance.new("Model", player)
	leaderstats.Name = "leaderstats"
	
	local KWA = Instance.new("StringValue", leaderstats)
	if Configurations.GRANT_ASSISTS then
		KWA.Name = "KO - WO - A"
		KWA.Value = "0 - 0 - 0"
	else
		KWA.Name = "KO - WO"
		KWA.Value = "0 - 0"
	end
	
	ScoreArray[player] = { 0, 0, 0 } -- KOs, WOs, Assists
	
	TeamManager:AssignPlayerToTeam(player)
	
	player.CharacterAdded:connect(function(character)
		character:WaitForChild("Humanoid").Died:connect(function()
			PlayerManager:PlayerDied(player)
			wait(Configurations.RESPAWN_TIME)
			if GameRunning then
				player:LoadCharacter()
			end
		end)
	end)
	
	if PlayersCanSpawn then
		player:LoadCharacter()
	else
		DisplayManager:StartIntermission(player)
	end
end

local function OnPlayerRemoving(player)
	TeamManager:RemovePlayer(player)
end

local function UpdatePlayerScore(player)
	local KOString = tostring(ScoreArray[player][1])
	local WOString = tostring(ScoreArray[player][2])
	local AssistString = tostring(ScoreArray[player][3])
	
	if not Configurations.GRANT_ASSISTS then
		if KOString:len() < WOString:len() then
			while KOString:len() < WOString:len() do
				KOString = " " .. KOString
			end
		else
			while WOString:len() < WOString:len() do
				WOString = WOString .. " "
			end
		end
		player.leaderstats["KO - WO"].Value = KOString .. " - " .. WOString
	else
		player.leaderstats["KO - WO - A"].Value = KOString .. " - " .. WOString .. " - " .. AssistString
	end
end
