-- NintendoZERG


function Entered(p)

if p:findFirstChild("leaderstats") == nil then

stats = Instance.new("IntValue")
stats.Parent = p
stats.Name = "leaderstats"

money = Instance.new("IntValue")
money.Value = 0 -- How much money each player starts with.
money.Name = "Money"
money.Parent = stats

ao = Instance.new("IntValue")
ao.Value = 0
ao.Name = "AlreadyOwned"
ao.Parent = p

end
end

game.Players.PlayerAdded:connect(Entered)

p = game.Players:GetChildren()
for i = 1, #p do
Entered(p[i])
end
print("LinkedLeaderboard script version 5.00 loaded")

stands = {}
CTF_mode = false

function handleKillCount(humanoid, player)
	local killer = getKillerOfHumanoidIfStillInGame(humanoid)
	if killer ~= nil then
		local stats = killer:findFirstChild("leaderstats")
		if stats ~= nil then
			local kills = stats:findFirstChild("KOs")
			if killer ~= player then
				kills.Value = kills.Value + 1
				
			else
				kills.Value = kills.Value - 1
				
			end
		end
	end
end

function onPlayerEntered(newPlayer)

	if CTF_mode == true then

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


		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

		kills.Parent = stats
		deaths.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
