Getting the teams of all the players in an array

Hello,

I’m currently working on a flag capture system, and I have an array of Players that are currently in the flags “zone”. I achieve this through doing:

for i, Part in pairs(PartsInRegion) do
		local Player = game.Players:GetPlayerFromCharacter(Part.Parent)
		
		if Player then
			for i, v in pairs(Players) do
				if Players[i].Name == Player.Name  then
					PlayerFound = true
				end
			end

			if PlayerFound == false then
				table.insert(Players, Player)
			end
			
			PlayerFound = false
		end
	end

Now I want to figure out how many players from each team are in the zone, so that I can work out if the zone is contested. But I’m unsure of how I can do this- if anyone could help me out I’d appreciate it!

Thanks

You can create a table for each team. For example, you can create a dictionary called Teams with arrays inside the dictionary representing all the teams. Before you start entering your loop, you can write something like this:

local Teams = {}
for i, Team in pairs(game.Teams:GetChildren()) do
	Teams[Team] = {}
end

Then when you are inserting the player to the Players array in this line,

you can add something like this:

table.insert(Teams[Player.Team],Player)

So once the loop finishes, you will have a dictionary structured something like this:

Teams = {
	TeamA = {Player1, Player2},
	TeamB = {Player 3, Player 4, Player 5},
	TeamC = {Player6}
}

Then I guess you could do whatever you want with that dictionary. Hope this helps!

1 Like