Get players of a team present in a certain zone on a screen

Hello !
I got this script supposed to take players present in the region3, insert them in a table, and then show the amount in the screen, but for some reasons, it doesn’t detect the player, here is the code :

local part = game.Workspace.CountZone
local min = Vector3.new(part.Position.X-part.Size.X/2, part.Position.Y-part.Size.Y/2, part.Position.Z-part.Size.Z/2)
local max= Vector3.new(part.Position.X+part.Size.X/2, part.Position.Y+part.Size.Y/2, part.Position.Z+part.Size.Z/2)
local Region = Region3.new(min, max)
local players = game:GetService("Players")
local Teams = game:GetService("Teams")
local playerMTF = {}
local playerCI = {}
local playerSCPs = {}

while wait(3) do
	local playerMTF = {}
	local playerCI = {}
	local playerSCPs = {}
	for _, part in pairs(game.Workspace:FindPartsInRegion3(Region,nil,math.huge)) do
		if players.Team == Teams.Alpha or players.Team == Teams.HammerDown or players.Team == Teams.NTF or players.Team == Teams.O5 or players.Team == Teams.Guard or players.Team == Teams.Scientist or players.Team == Teams.SeeNoEvil then
			local hum = part.Parent:FindFirstChild("Humanoid")
			if not hum or table.find(playerMTF,part.Parent.Name) then continue end
			table.insert(playerMTF, part.Parent.Name)
			game.Workspace.WarheadControl.Screen.SurfaceGui.MainFrame.ThreatFrame.VisitorsNumLabel.Text = #playerMTF
		elseif players.Team == Teams.ChaosI then
			local hum = part.Parent:FindFirstChild("Humanoid")
			if not hum or table.find(playerCI,part.Parent.Name) then continue end
			table.insert(playerCI, part.Parent.Name)
			game.Workspace.WarheadControl.Screen.SurfaceGui.MainFrame.ThreatFrame.CINumLabel.Text = #playerCI
		end
	end
end

You’re trying to get the Team property from the Players Service, you’re on the right track, just change your code around so it gets the player via players:GetPlayerFromCharacter(hit.Parent) in that loop

Can’t use it cuz it underline it, I only have part but it get the model, not the player

Right, you’re using part as the 2nd argument and not hit, my bad on that one, try something like this?

local part = game.Workspace.CountZone
local min = Vector3.new(part.Position.X-part.Size.X/2, part.Position.Y-part.Size.Y/2, part.Position.Z-part.Size.Z/2)
local max= Vector3.new(part.Position.X+part.Size.X/2, part.Position.Y+part.Size.Y/2, part.Position.Z+part.Size.Z/2)
local Region = Region3.new(min, max)
local players = game:GetService("Players")
local Teams = game:GetService("Teams")
local playerMTF = {}
local playerCI = {}
local playerSCPs = {}

while wait(3) do
	local playerMTF = {}
	local playerCI = {}
	local playerSCPs = {}
	for _, part in pairs(game.Workspace:FindPartsInRegion3(Region,nil,math.huge)) do
		local plr = players:GetPlayerFromCharacter(part.Parent)
		if not plr then continue end
		if plr.Team == Teams.Alpha or plr.Team == Teams.HammerDown or plr.Team == Teams.NTF or plr.Team == Teams.O5 or plr.Team == Teams.Guard or plr.Team == Teams.Scientist or plr.Team == Teams.SeeNoEvil then
			if table.find(playerMTF, plr.Name) then continue end
			table.insert(playerMTF, plr.Name)
			game.Workspace.WarheadControl.Screen.SurfaceGui.MainFrame.ThreatFrame.VisitorsNumLabel.Text = #playerMTF
		elseif plr.Team == Teams.ChaosI then
			if table.find(playerCI, plr.Name) then continue end
			table.insert(playerCI, plr.Name)
			game.Workspace.WarheadControl.Screen.SurfaceGui.MainFrame.ThreatFrame.CINumLabel.Text = #playerCI
		end
	end
end

Also please use a table for that first if statement, it’llhelp reduce the spaces of it

Thanks!
It count the player, but when he leaves the zone, it doesn’t remove his name in the table, how can I fix it?

Perhaps put the text changing lines after the FindPartsInregion3 loop?

local part = game.Workspace.CountZone
local min = Vector3.new(part.Position.X-part.Size.X/2, part.Position.Y-part.Size.Y/2, part.Position.Z-part.Size.Z/2)
local max= Vector3.new(part.Position.X+part.Size.X/2, part.Position.Y+part.Size.Y/2, part.Position.Z+part.Size.Z/2)
local Region = Region3.new(min, max)
local players = game:GetService("Players")
local Teams = game:GetService("Teams")
local playerMTF = {}
local playerCI = {}
local playerSCPs = {}

while wait(3) do
	local playerMTF = {}
	local playerCI = {}
	local playerSCPs = {}
	for _, part in pairs(game.Workspace:FindPartsInRegion3(Region,nil,math.huge)) do
		local plr = players:GetPlayerFromCharacter(part.Parent)
		if not plr then continue end
		if plr.Team == Teams.Alpha or plr.Team == Teams.HammerDown or plr.Team == Teams.NTF or plr.Team == Teams.O5 or plr.Team == Teams.Guard or plr.Team == Teams.Scientist or plr.Team == Teams.SeeNoEvil then
			if table.find(playerMTF, plr.Name) then continue end
			table.insert(playerMTF, plr.Name)
		elseif plr.Team == Teams.ChaosI then
			if table.find(playerCI, plr.Name) then continue end
			table.insert(playerCI, plr.Name)
		end
	end
	game.Workspace.WarheadControl.Screen.SurfaceGui.MainFrame.ThreatFrame.CINumLabel.Text = #playerCI
	game.Workspace.WarheadControl.Screen.SurfaceGui.MainFrame.ThreatFrame.VisitorsNumLabel.Text = #playerMTF
end
1 Like