[Solved] Array/Dictionary Help

Hello,

I’m trying to make a capture point system, and have got to the point where I can track the players that are inside the capture points “zone” by adding the player to a table when they enter. Though the the table works currently looks like this:

{
	["Team 1"] = {
		["Player name"] = UserId,
	}
	
	["Team 2"] = {
		["Player name"] = UserId,
	}
}

But what I would like to do is have it look like:

{
	["Team 1"] = 1, -- Value depends on how many players from the team are in the zone
	["Team 2"] = 0,
}

But I’m unsure of how I could do it. Here is my current code:

local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

function PlayerTracker()
	local PlayerTracker = {}

	for _, Part in pairs(CollectionService:GetTagged("Hardpoint")) do task.wait()
		local TouchingParts = workspace:GetPartsInPart(Part)

		for I, TouchingPart in pairs(TouchingParts) do task.wait()
			local Player

			local Humanoid = TouchingPart.Parent:FindFirstChild("Humanoid")
			if Humanoid then
				local Player = Players:GetPlayerFromCharacter(TouchingPart.Parent)
				if Player then
					local PlayerJoinedTable = PlayerTracker[Player.Team.Name] or {};
					PlayerTracker[Player.Team.Name] = PlayerJoinedTable
					PlayerJoinedTable[Player.Name] = Player.UserId
				end
			end
		end
	end

	return PlayerTracker
end

local TrackPlayerHandle = RunService.Heartbeat:Connect(function()
	print(PlayerTracker())
end)

If anyone could point me in the right direction I would greatly appreciate it!

Thanks

You make tables that adds people into that table, and make it so that they cannot be added twice to any table.
Then you get a sum of those tables for each team.

1 Like

You have to loop over the nested dictionary to count the keys to get the team number count.

1 Like

I mean that you add the player id’s into tables for teams, then you get a sum for each of those tables to get the amount of people per team

1 Like

For ease, let’s call this table 1.
So:

table1 = {
	["Team 1"] = {
		["Player name"] = UserId,
	},
	
	["Team 2"] = {
		["Player name"] = UserId,
	}
}

To get this to your table2:

do:

table2 = {}
for team, plrTable in pairs(table1) do
    if table2[team] == nil then table2[team] = 0 end
    for plrName, userId in pairs(plrTable) do
        table2[team] += 1
    end
end

Now the table2 looks exactly as you wanted it to!

1 Like