local J={}local s=game.Players local p=game:GetService("\080\111\105\110\116\115\083\101\114\118\105\099\101")local Z=require(game.ServerStorage.Configurations)local d=game.ReplicatedStorage.Events local t=d.ResetMouseIcon local F=require(script.Parent.TeamManager)local G=require(script.Parent.DisplayManager)local S=false local C=false local function q(J)local s=Instance.new("\077\111\100\101\108",J)s.Name="\108\101\097\100\101\114\115\116\097\116\115"local p=Instance.new("\073\110\116\086\097\108\117\101",s)p.Name="\067\097\112\116\117\114\101\115"p.Value=0 F:AssignPlayerToTeam(J)J.CharacterAdded:connect(function(s)(s:WaitForChild("\072\117\109\097\110\111\105\100")).Died:connect(function()wait(Z.RESPAWN_TIME)if C then J:LoadCharacter()end end)end)if S then J:LoadCharacter()else G:StartIntermission(J)end end local function a(J)F:RemovePlayer(J)end function J.SetGameRunning(s,J)C=J end function J.ClearPlayerScores(J)for J,s in ipairs(s:GetPlayers())do local p=s:FindFirstChild("\108\101\097\100\101\114\115\116\097\116\115")if p then local J=p:FindFirstChild("\067\097\112\116\117\114\101\115")if J then J.Value=0 end end end end function J.LoadPlayers(J)for J,s in ipairs(s:GetPlayers())do s:LoadCharacter()end end function J.AllowPlayerSpawn(s,J)S=J end function J.DestroyPlayers(J)for J,s in ipairs(s:GetPlayers())do s.Character:Destroy()for J,s in ipairs(s.Backpack:GetChildren())do s:Destroy()end end t:FireAllClients()end function J.AddPlayerScore(Z,J,s)J.leaderstats.Captures.Value=J.leaderstats.Captures.Value+s local d,t=pcall(function()spawn(function()p:AwardPoints(J.userId,s)end)end)end s.PlayerAdded:connect(q)s.PlayerRemoving:connect(a)return J
local PlayerManager = {}

-- ROBLOX Services
local Players = game.Players
local PointsService = game:GetService('PointsService')

-- Game services
local Configurations = require(game.ServerStorage.Configurations)
local Events = game.ReplicatedStorage.Events
local ResetMouseIcon = Events.ResetMouseIcon
local TeamManager = require(script.Parent.TeamManager)
local DisplayManager = require(script.Parent.DisplayManager)

-- Local variables
local PlayersCanSpawn = false
local GameRunning = false

-- Local Functions
local function OnPlayerAdded(player)
	-- Setup leaderboard stats
	local leaderstats = Instance.new('Model', player)
	leaderstats.Name = 'leaderstats'
	
	local Captures = Instance.new('IntValue', leaderstats)
	Captures.Name = 'Captures'
	Captures.Value = 0
	
	-- Add player to team
	TeamManager:AssignPlayerToTeam(player)
	
	player.CharacterAdded:connect(function(character)
		character:WaitForChild('Humanoid').Died:connect(function()
			wait(Configurations.RESPAWN_TIME)
			if GameRunning then
				player:LoadCharacter()
			end
		end)
	end)	
	
	-- Check if player should be spawned	
	if PlayersCanSpawn then
		player:LoadCharacter()
	else
		DisplayManager:StartIntermission(player)
	end	

end

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

-- Public Functions

function PlayerManager:SetGameRunning(running)
	GameRunning = running
end

function PlayerManager:ClearPlayerScores()
	for _, player in ipairs(Players:GetPlayers()) do
		local leaderstats = player:FindFirstChild('leaderstats')
		if leaderstats then
			local Captures = leaderstats:FindFirstChild('Captures')
			if Captures then
				Captures.Value = 0
			end
		end
	end
end

function PlayerManager:LoadPlayers()
	for _, player in ipairs(Players:GetPlayers()) do
		player:LoadCharacter()
	end
end

function PlayerManager:AllowPlayerSpawn(allow)
	PlayersCanSpawn = allow
end

function PlayerManager:DestroyPlayers()
	for _, player in ipairs(Players:GetPlayers()) do
		player.Character:Destroy()
		for _, item in ipairs(player.Backpack:GetChildren()) do
			item:Destroy()
		end
	end
	ResetMouseIcon:FireAllClients()
end

function PlayerManager:AddPlayerScore(player, score)
	player.leaderstats.Captures.Value = player.leaderstats.Captures.Value + score
	local success, message = pcall(function() spawn(function() PointsService:AwardPoints(player.userId, score) end) end)
end

-- Event binding
Players.PlayerAdded:connect(OnPlayerAdded)
Players.PlayerRemoving:connect(OnPlayerRemoving)

return PlayerManager