local J={}local s=game.Players local p=require(game.ServerStorage.Configurations)local Z={}local d={}local t=game.ReplicatedStorage.Events local F=t.CaptureFlag local G=t.ReturnFlag local S local function C(J)J.Flag:Destroy()for s,p in pairs(d)do if p==J then d[s]=nil end end end local function q(J)local s=d[J]if s then local Z=s.FlagPole local t=s.FlagBanner Z.CanCollide=false t.CanCollide=false Z.Anchored=true t.Anchored=true s.PickedUp=false d[J]=nil if p.RETURN_FLAG_ON_DROP then wait(p.FLAG_RESPAWN_TIME)if not s.AtSpawn and not s.PickedUp then C(s)S(s)G:Fire(s.FlagBanner.BrickColor)end end end end local function a(J,s)d[J]=s s.AtSpawn=false s.PickedUp=true local p if J.Character.Humanoid.RigType==Enum.HumanoidRigType.R6 then p=J.Character:FindFirstChild("\084\111\114\115\111")else p=J.Character:FindFirstChild("\085\112\112\101\114\084\111\114\115\111")end local Z=s.FlagPole local t=s.FlagBanner Z.Anchored=false t.Anchored=false Z.CanCollide=false t.CanCollide=false local F=Instance.new("\087\101\108\100",Z)F.Name="\080\108\097\121\101\114\070\108\097\103\087\101\108\100"F.Part0=Z F.Part1=p F.C0=CFrame.new(0,0,-1)end local function v(J)local Z=J.FlagPole local d=J.FlagBanner Z.Touched:connect(function(Z)local t=s:GetPlayerFromCharacter(Z.Parent)if not t then return end if not t.Character then return end local F=t.Character:FindFirstChild("\072\117\109\097\110\111\105\100")if not F then return end if F.Health<=0 then return end if d.BrickColor~=t.TeamColor and not J.PickedUp then a(t,J)elseif d.BrickColor==t.TeamColor and(not J.AtSpawn and p.FLAG_RETURN_ON_TOUCH)then C(J)S(J)G:Fire(J.FlagBanner.BrickColor)end end)end function S(J)J.Flag=J.FlagCopy:Clone()J.Flag.Parent=J.FlagContainer J.FlagPole=J.Flag.FlagPole J.FlagBanner=J.Flag.FlagBanner J.FlagBanner.CanCollide=false J.AtSpawn=true J.PickedUp=false v(J)end local function I(J)local p=J.FlagBase p.Touched:connect(function(J)local Z=s:GetPlayerFromCharacter(J.Parent)if not Z then return end if p.BrickColor==Z.TeamColor and d[Z]then F:Fire(Z)local J=d[Z]C(J)S(J)end end)end local function E(J)J.CharacterAdded:connect(function(s)(s:WaitForChild("\072\117\109\097\110\111\105\100")).Died:connect(function()q(J)end)end)J.CharacterRemoving:connect(function()q(J)end)end function J.Init(s,J)local p={}local d,t=pcall(function()p.AtSpawn=true p.PickedUp=false p.TeamColor=J.FlagStand.BrickColor p.Flag=J.Flag p.FlagPole=J.Flag.FlagPole p.FlagBanner=J.Flag.FlagBanner p.FlagBase=J.FlagStand p.FlagCopy=J.Flag:Clone()p.FlagContainer=J end)if not d then warn("\070\108\097\103 \111\098\106\101\099\116 \110\111\116 \098\117\105\108\116 \099\111\114\114\101\099\116\108\121\046 \080\108\101\097\115\101 \108\111\097\100 \102\114\101\115\104 \116\101\109\112\108\097\116\101 \116\111 \115\101\101 \104\111\119 \102\108\097\103 \115\116\097\110\100 \105\115 \101\120\112\101\099\116\101\100 \116\111 \098\101 \109\097\100\101\046")end I(p)C(p)S(p)table.insert(Z,p)end s.PlayerAdded:connect(E)return J


local FlagStandManager = {}

-- ROBLOX Services
local Players = game.Players

-- Game Services
local Configurations = require(game.ServerStorage.Configurations)

-- Local Variables
local FlagObjects = {}
local FlagCarriers = {}
local Events = game.ReplicatedStorage.Events
local CaptureFlag = Events.CaptureFlag
local ReturnFlag = Events.ReturnFlag

-- Local Functions
local MakeFlag

local function DestroyFlag(flagObject)
	flagObject.Flag:Destroy()
	for player, object in pairs(FlagCarriers) do
		if object == flagObject then
			FlagCarriers[player] = nil
		end
	end
end

local function OnCarrierDied(player)
	local flagObject = FlagCarriers[player]
	if flagObject then
		local flagPole = flagObject.FlagPole
		local flagBanner = flagObject.FlagBanner
		
		flagPole.CanCollide = false
		flagBanner.CanCollide = false
		flagPole.Anchored = true
		flagBanner.Anchored = true
		
		flagObject.PickedUp = false		
		
		FlagCarriers[player] = nil
		
		if Configurations.RETURN_FLAG_ON_DROP then
			wait(Configurations.FLAG_RESPAWN_TIME)
			if not flagObject.AtSpawn and not flagObject.PickedUp then
				DestroyFlag(flagObject)
				MakeFlag(flagObject)
				ReturnFlag:Fire(flagObject.FlagBanner.BrickColor)
			end
		end
	end
end

local function PickupFlag(player, flagObject)
	FlagCarriers[player] = flagObject
	flagObject.AtSpawn = false
	flagObject.PickedUp = true
	
	local torso
	if player.Character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
		torso = player.Character:FindFirstChild('Torso')
	else
		torso = player.Character:FindFirstChild('UpperTorso')
	end
	local flagPole = flagObject.FlagPole
	local flagBanner = flagObject.FlagBanner
	
	flagPole.Anchored = false
	flagBanner.Anchored = false
	flagPole.CanCollide = false
	flagBanner.CanCollide = false
	local weld = Instance.new('Weld', flagPole)
	weld.Name = 'PlayerFlagWeld'
	weld.Part0 = flagPole
	weld.Part1 = torso
	weld.C0 = CFrame.new(0,0,-1)
end

local function BindFlagTouched(flagObject)
	local flagPole = flagObject.FlagPole
	local flagBanner = flagObject.FlagBanner
	flagPole.Touched:connect(function(otherPart)
		local player = Players:GetPlayerFromCharacter(otherPart.Parent)
		if not player then return end
		if not player.Character then return end
		local humanoid = player.Character:FindFirstChild('Humanoid')
		if not humanoid then return end
		if humanoid.Health <= 0 then return end
		if flagBanner.BrickColor ~= player.TeamColor and not flagObject.PickedUp then
			PickupFlag(player, flagObject)
		elseif flagBanner.BrickColor == player.TeamColor and not flagObject.AtSpawn and Configurations.FLAG_RETURN_ON_TOUCH then
			DestroyFlag(flagObject)
			MakeFlag(flagObject)
			ReturnFlag:Fire(flagObject.FlagBanner.BrickColor)
		end
	end)
end

function MakeFlag(flagObject)
	flagObject.Flag = flagObject.FlagCopy:Clone()
	flagObject.Flag.Parent = flagObject.FlagContainer
	flagObject.FlagPole = flagObject.Flag.FlagPole
	flagObject.FlagBanner = flagObject.Flag.FlagBanner
	flagObject.FlagBanner.CanCollide = false
	flagObject.AtSpawn = true
	flagObject.PickedUp = false
	BindFlagTouched(flagObject)
end

local function BindBaseTouched(flagObject)
	local flagBase = flagObject.FlagBase
	flagBase.Touched:connect(function(otherPart)
		local player = Players:GetPlayerFromCharacter(otherPart.Parent)
		if not player then return end
		if flagBase.BrickColor == player.TeamColor and FlagCarriers[player] then
			CaptureFlag:Fire(player)
			local otherFlag = FlagCarriers[player]
			DestroyFlag(otherFlag)
			MakeFlag(otherFlag)
		end
	end)
end

local function OnPlayerAdded(player)
	player.CharacterAdded:connect(function(character)
		character:WaitForChild('Humanoid').Died:connect(function() OnCarrierDied(player) end)
	end)
	player.CharacterRemoving:connect(function()
		OnCarrierDied(player)
	end)
end

-- Public Functions
function FlagStandManager:Init(container)
	local flagObject = {}
	
	local success, message = pcall(function()
		flagObject.AtSpawn = true	
		flagObject.PickedUp = false
		flagObject.TeamColor = container.FlagStand.BrickColor
		flagObject.Flag = container.Flag
		flagObject.FlagPole = container.Flag.FlagPole
		flagObject.FlagBanner = container.Flag.FlagBanner
		flagObject.FlagBase = container.FlagStand
		flagObject.FlagCopy = container.Flag:Clone()	
		flagObject.FlagContainer = container
	end)
	if not success then
		warn("Flag object not built correctly. Please load fresh template to see how flag stand is expected to be made.")
	end
	
	BindBaseTouched(flagObject)
	DestroyFlag(flagObject)
	MakeFlag(flagObject)
	
	table.insert(FlagObjects, flagObject)
end

-- Event Bindings
Players.PlayerAdded:connect(OnPlayerAdded)

return FlagStandManager
