function findAllFlagStands(P)local O=P:children()for P=1,#O,1 do if O[P].className=="\077\111\100\101\108"or O[P].className=="\080\097\114\116"then findAllFlagStands(O[P])end if O[P].className=="\070\108\097\103\083\116\097\110\100"then table.insert(stands,O[P])end end end function hookUpListeners()for P=1,#stands,1 do stands[P].FlagCaptured:connect(onCaptureScored)end end function onPlayerEntered(P)if CTF_mode==true then local O=Instance.new("\070\111\108\100\101\114")O.Name="\108\101\097\100\101\114\115\116\097\116\115"local Q=Instance.new("\073\110\116\086\097\108\117\101")Q.Name="\067\097\112\116\117\114\101\115"Q.Value=0 Q.Parent=O while true do if P.Character~=nil then break end wait(5)end O.Parent=P else local O=Instance.new("\070\111\108\100\101\114")O.Name="\108\101\097\100\101\114\115\116\097\116\115"local Q=false if Settings.LeaderboardSettings.KOs then Q=Instance.new("\073\110\116\086\097\108\117\101")Q.Name=Settings.LeaderboardSettings.KillsName Q.Value=0 end local G=false if Settings.LeaderboardSettings.WOs then G=Instance.new("\073\110\116\086\097\108\117\101")G.Name=Settings.LeaderboardSettings.DeathsName G.Value=0 end local v=false if Settings.LeaderboardSettings.ShowCurrency then v=Instance.new("\073\110\116\086\097\108\117\101")v.Name=Settings.CurrencyName v.Value=0 end local T=game.ServerStorage.PlayerMoney:FindFirstChild(P.Name)if T~=nil then if v then local P=Settings.LeaderboardSettings.ShowShortCurrency T.Changed:connect(function()if P then v.Value=Settings:ConvertShort(T.Value)else v.Value=Settings:ConvertComma(T.Value)end end)end end if Q then Q.Parent=O end if G then G.Parent=O end if v then v.Parent=O end while true do if P.Character~=nil then break end wait(5)end local n=P.Character.Humanoid n.Died:connect(function()onHumanoidDied(n,P)end)P.Changed:connect(function(O)onPlayerRespawn(O,P)end)O.Parent=P end end function onCaptureScored(P)local O=P:findFirstChild("\108\101\097\100\101\114\115\116\097\116\115")if O==nil then return end local Q=O:findFirstChild("\067\097\112\116\117\114\101\115")if Q==nil then return end Q.Value=Q.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("Folder")
		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("Folder")
		stats.Name = "leaderstats"
		local kills = false
		if Settings.LeaderboardSettings.KOs then
			kills = Instance.new("IntValue")
			kills.Name = Settings.LeaderboardSettings.KillsName
			kills.Value = 0
		end
		local deaths = false
		if Settings.LeaderboardSettings.WOs then
			deaths = Instance.new("IntValue")
			deaths.Name = Settings.LeaderboardSettings.DeathsName
			deaths.Value = 0
		end

		local cash = false
		if Settings.LeaderboardSettings.ShowCurrency then
			cash = Instance.new("IntValue")
			cash.Name = Settings.CurrencyName
			cash.Value = 0
		end

		local PlayerStats = game.ServerStorage.PlayerMoney:FindFirstChild(newPlayer.Name)
		if PlayerStats ~= nil then
			if cash then
				local Short = Settings.LeaderboardSettings.ShowShortCurrency
				PlayerStats.Changed:connect(function()
					if Short then
						cash.Value = Settings:ConvertShort(PlayerStats.Value)
					else
						cash.Value = Settings:ConvertComma(PlayerStats.Value)
					end
				end)
			end
		end
		if kills then
			kills.Parent = stats
		end
		if deaths then
			deaths.Parent = stats
		end
		if cash then
			cash.Parent = stats
		end

		-- 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)
