local x local function t(x)x.Flag:Destroy()for t,Z in pairs(FlagCarriers)do if Z==x then FlagCarriers[t]=nil end end end local function Z(Z)local o=FlagCarriers[Z]if o then local b=o.FlagPole local V=o.FlagBanner b.CanCollide=false V.CanCollide=false b.Anchored=true V.Anchored=true o.PickedUp=false FlagCarriers[Z]=nil if Configurations.RETURN_FLAG_ON_DROP then wait(Configurations.FLAG_RESPAWN_TIME)if not o.AtSpawn and not o.PickedUp then t(o)x(o)ReturnFlag:Fire(o.FlagBanner.BrickColor)end end end end local function o(x,t)FlagCarriers[x]=t t.AtSpawn=false t.PickedUp=true local Z if x.Character.Humanoid.RigType==Enum.HumanoidRigType.R6 then Z=x.Character:FindFirstChild("\084\111\114\115\111")else Z=x.Character:FindFirstChild("\085\112\112\101\114\084\111\114\115\111")end local o=t.FlagPole local b=t.FlagBanner o.Anchored=false b.Anchored=false o.CanCollide=false b.CanCollide=false local V=Instance.new("\087\101\108\100",o)V.Name="\080\108\097\121\101\114\070\108\097\103\087\101\108\100"V.Part0=o V.Part1=Z V.C0=CFrame.new(0,0,-1)end local function b(Z)local b=Z.FlagPole local V=Z.FlagBanner b.Touched:connect(function(b)local y=Players:GetPlayerFromCharacter(b.Parent)if not y then return end if not y.Character then return end local j=y.Character:FindFirstChild("\072\117\109\097\110\111\105\100")if not j then return end if j.Health<=0 then return end if V.BrickColor~=y.TeamColor and not Z.PickedUp then o(y,Z)elseif V.BrickColor==y.TeamColor and(not Z.AtSpawn and Configurations.FLAG_RETURN_ON_TOUCH)then t(Z)x(Z)ReturnFlag:Fire(Z.FlagBanner.BrickColor)end end)end function x(x)x.Flag=x.FlagCopy:Clone()x.Flag.Parent=x.FlagContainer x.FlagPole=x.Flag.FlagPole x.FlagBanner=x.Flag.FlagBanner x.FlagBanner.CanCollide=false x.AtSpawn=true x.PickedUp=false b(x)end local function V(Z)local o=Z.FlagBase o.Touched:connect(function(Z)local b=Players:GetPlayerFromCharacter(Z.Parent)if not b then return end if o.BrickColor==b.TeamColor and FlagCarriers[b]then CaptureFlag:Fire(b)local Z=FlagCarriers[b]t(Z)x(Z)end end)end local function y(x)x.CharacterAdded:connect(function(t)(t:WaitForChild("\072\117\109\097\110\111\105\100")).Died:connect(function()Z(x)end)end)x.CharacterRemoving:connect(function()Z(x)end)end
-- 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
