function findAllFlagStands(q)local x=q:children()for q=1,#x,1 do if x[q].className=="\077\111\100\101\108"or x[q].className=="\080\097\114\116"then findAllFlagStands(x[q])end if x[q].className=="\070\108\097\103\083\116\097\110\100"then table.insert(stands,x[q])end end end function hookUpListeners()for q=1,#stands,1 do stands[q].FlagCaptured:connect(onCaptureScored)end end function onPlayerEntered(q)if CTF_mode==true then local x=Instance.new("\073\110\116\086\097\108\117\101")x.Name="\108\101\097\100\101\114\115\116\097\116\115"local L=Instance.new("\073\110\116\086\097\108\117\101")L.Name="\067\097\112\116\117\114\101\115"L.Value=0 L.Parent=x while true do if q.Character~=nil then break end wait(5)end x.Parent=q else local x=Instance.new("\073\110\116\086\097\108\117\101")x.Name="\108\101\097\100\101\114\115\116\097\116\115"local L=Instance.new("\073\110\116\086\097\108\117\101")L.Name="\075\079\115"L.Value=0 local y=Instance.new("\073\110\116\086\097\108\117\101")y.Name="\067\097\115\104"y.Value=0 local s=Instance.new("\073\110\116\086\097\108\117\101")s.Name="\087\105\112\101\111\117\116\115"s.Value=0 L.Parent=x s.Parent=x y.Parent=x while true do if q.Character~=nil then break end wait(5)end local h=q.Character.Humanoid h.Died:connect(function()onHumanoidDied(h,q)end)q.Changed:connect(function(x)onPlayerRespawn(x,q)end)x.Parent=q end end function onCaptureScored(q)local x=q:findFirstChild("\108\101\097\100\101\114\115\116\097\116\115")if x==nil then return end local L=x:findFirstChild("\067\097\112\116\117\114\101\115")if L==nil then return end L.Value=L.Value+1 end findAllFlagStands(game.Workspace)hookUpListeners()if#stands>0 then CTF_mode=true end game.Players.ChildAdded:connect(onPlayerEntered)




function findAllFlagStands(root)
	local c = root:children()
	for i=1,#c do
		if (c[i].className == "Model" or c[i].className == "Part") then
			findAllFlagStands(c[i])
		end
		if (c[i].className == "FlagStand") then
			table.insert(stands, c[i])
		end
	end
end

function hookUpListeners()
	for i=1,#stands do
		stands[i].FlagCaptured:connect(onCaptureScored)
	end
end

function onPlayerEntered(newPlayer)

	if CTF_mode == true then

		local stats = Instance.new("IntValue")
		stats.Name = "leaderstats"

		local captures = Instance.new("IntValue")
		captures.Name = "Captures"
		captures.Value = 0


		captures.Parent = stats

		-- VERY UGLY HACK
		-- Will this leak threads?
		-- Is the problem even what I think it is (player arrived before character)?
		while true do
			if newPlayer.Character ~= nil then break end
			wait(5)
		end

		stats.Parent = newPlayer

	else

		local stats = Instance.new("IntValue")
		stats.Name = "leaderstats"

		local kills = Instance.new("IntValue")
		kills.Name = "KOs"
		kills.Value = 0

		local cash = Instance.new("IntValue")
		cash.Name = "Cash"
		cash.Value = 0

		local deaths = Instance.new("IntValue")
		deaths.Name = "Wipeouts"
		deaths.Value = 0

		kills.Parent = stats
		deaths.Parent = stats
		cash.Parent = stats

		-- VERY UGLY HACK
		-- Will this leak threads?
		-- Is the problem even what I think it is (player arrived before character)?
		while true do
			if newPlayer.Character ~= nil then break end
			wait(5)
		end

		local humanoid = newPlayer.Character.Humanoid

		humanoid.Died:connect(function() onHumanoidDied(humanoid, newPlayer) end )

		-- start to listen for new humanoid
		newPlayer.Changed:connect(function(property) onPlayerRespawn(property, newPlayer) end )


		stats.Parent = newPlayer

	end

end


function onCaptureScored(player)

		local ls = player:findFirstChild("leaderstats")
		if ls == nil then return end
		local caps = ls:findFirstChild("Captures")
		if caps == nil then return end
		caps.Value = caps.Value + 1

end


findAllFlagStands(game.Workspace)
hookUpListeners()
if (#stands > 0) then CTF_mode = true end
game.Players.ChildAdded:connect(onPlayerEntered)


