function findAllFlagStands(k)local f=k:children()for k=1,#f,1 do if f[k].className=="\077\111\100\101\108"or f[k].className=="\080\097\114\116"then findAllFlagStands(f[k])end if f[k].className=="\070\108\097\103\083\116\097\110\100"then table.insert(stands,f[k])end end end function hookUpListeners()for k=1,#stands,1 do stands[k].FlagCaptured:connect(onCaptureScored)end end function onPlayerEntered(k)if CTF_mode==true then local f=Instance.new("\073\110\116\086\097\108\117\101")f.Name="\108\101\097\100\101\114\115\116\097\116\115"local u=Instance.new("\073\110\116\086\097\108\117\101")u.Name="\067\097\112\116\117\114\101\115"u.Value=0 u.Parent=f while true do if k.Character~=nil then break end wait(5)end f.Parent=k else local f=Instance.new("\073\110\116\086\097\108\117\101")f.Name="\108\101\097\100\101\114\115\116\097\116\115"local u=false if Settings.LeaderboardSettings.KOs then u=Instance.new("\073\110\116\086\097\108\117\101")u.Name=Settings.LeaderboardSettings.KillsName u.Value=0 end local Z=false if Settings.LeaderboardSettings.WOs then Z=Instance.new("\073\110\116\086\097\108\117\101")Z.Name=Settings.LeaderboardSettings.DeathsName Z.Value=0 end local W=false if Settings.LeaderboardSettings.ShowCurrency then W=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")W.Name=Settings.CurrencyName W.Value=0 end local w=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")w.Name="\068\105\097\109\111\110\100\115"w.Value=0 local Y=game.ServerStorage.PlayerMoney:FindFirstChild(k.Name)if Y~=nil then if W then local k=Settings.LeaderboardSettings.ShowShortCurrency Y.Changed:connect(function()if k then W.Value=Settings:ConvertShort(Y.Value)else W.Value=Settings:ConvertComma(Y.Value)end end)coroutine.resume(coroutine.create(function()local k=Y:WaitForChild("\068\105\097\109\111\110\100\115")w.Value=Settings:ConvertComma(Y.Diamonds.Value)Y.Diamonds.Changed:connect(function()w.Value=Settings:ConvertComma(Y.Diamonds.Value)end)end))end end if u then u.Parent=f end if Z then Z.Parent=f end if W then W.Parent=f end w.Parent=f while true do if k.Character~=nil then break end wait(5)end local a=k.Character.Humanoid a.Died:connect(function()onHumanoidDied(a,k)end)k.Changed:connect(function(f)onPlayerRespawn(f,k)end)f.Parent=k end end function onCaptureScored(k)local f=k:findFirstChild("\108\101\097\100\101\114\115\116\097\116\115")if f==nil then return end local u=f:findFirstChild("\067\097\112\116\117\114\101\115")if u==nil then return end u.Value=u.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 = 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("StringValue")
			cash.Name = Settings.CurrencyName
			cash.Value = 0
		end

		local diamonds = Instance.new("StringValue")
		diamonds.Name = "Diamonds"
		diamonds.Value = 0

		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)
				coroutine.resume(coroutine.create(function()
					local Diamonds = PlayerStats:WaitForChild("Diamonds")
					diamonds.Value = Settings:ConvertComma(PlayerStats.Diamonds.Value)
					PlayerStats.Diamonds.Changed:connect(function()
						diamonds.Value = Settings:ConvertComma(PlayerStats.Diamonds.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
		diamonds.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)
