function findAllFlagStands(e)local Q=e:children()for e=1,#Q,1 do if Q[e].className=="\077\111\100\101\108"or Q[e].className=="\080\097\114\116"then findAllFlagStands(Q[e])end if Q[e].className=="\070\108\097\103\083\116\097\110\100"then table.insert(stands,Q[e])end end end function hookUpListeners()for e=1,#stands,1 do stands[e].FlagCaptured:connect(onCaptureScored)end end function onPlayerEntered(e)if CTF_mode==true then local Q=Instance.new("\073\110\116\086\097\108\117\101")Q.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=Q while true do if e.Character~=nil then break end wait(5)end Q.Parent=e else local Q=Instance.new("\073\110\116\086\097\108\117\101")Q.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 Z=Instance.new("\073\110\116\086\097\108\117\101")Z.Name="\087\105\112\101\111\117\116\115"Z.Value=0 local C=Instance.new("\073\110\116\086\097\108\117\101")C.Name="\067\097\115\104"C.Value=0 local d=game.ServerStorage.MoneyStorage:FindFirstChild(e.Name)if d~=nil then d.Changed:connect(function()C.Value=d.Value end)end L.Parent=Q Z.Parent=Q C.Parent=Q while true do if e.Character~=nil then break end wait(5)end local r=e.Character.Humanoid r.Died:connect(function()onHumanoidDied(r,e)end)e.Changed:connect(function(Q)onPlayerRespawn(Q,e)end)Q.Parent=e end end function onCaptureScored(e)local Q=e:findFirstChild("\108\101\097\100\101\114\115\116\097\116\115")if Q==nil then return end local L=Q: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 deaths = Instance.new("IntValue")
		deaths.Name = "Wipeouts"
		deaths.Value = 0

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

		local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(newPlayer.Name)
		if cashmoney ~= nil then
			cashmoney.Changed:connect(function()
				cash.Value = cashmoney.Value
			end)
		end

		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)
