Tracking Players in zones

Hello,

I’ve made a few posts about this, but I’m still trying different methods on how to track people that enter and leave a part/zone, so that I can eventually work out how many people from each team are in it. Kind of like a flag capture/king of the hill sort of thing.

The code currently looks like:

local playersInTable = {}

local playerDetector = game.Workspace.Hardpoint.PlayerDetector
local overlapParams = OverlapParams.new()

overlapParams.FilterDescendantsInstances = {playerDetector}
overlapParams.FilterType = Enum.RaycastFilterType.Blacklist

while wait(1) do
	local possiblePlayersInPart = workspace:GetPartBoundsInBox(playerDetector.CFrame, playerDetector.Size, overlapParams)
	
	local temp = {}
	
	for i, v in pairs(possiblePlayersInPart) do
		local modelAncestor = v:FindFirstAncestorOfClass("Model")
		if not modelAncestor then continue end
		if modelAncestor:FindFirstChild("Humanoid") then
			local player = game.Players:GetPlayerFromCharacter(modelAncestor)
			if playersInTable[player] then
				continue
			else
				temp[#temp + 1] = player
			end
		end
	end
	
	for index,player in pairs(playersInTable) do
		if not temp[player] then
			table.remove(playersInTable,index)
		end
	end
	
	playersInTable = {unpack(temp), unpack(playersInTable)}
	print(repr(playersInTable, {pretty=true}))
	table.clear(temp)
end

Though the issue I’m having is that the table wont hold more than one player at a time- If anyone has any ideas how I can fix it, I’d appreciate any help you could give me. I’d also like to mention that I’m not interested in using ZonePlus for this.

Thanks

Ok so I’ve been working on it a little, and my code now looks like this:

local repr = require(3148021300)

local PlayerFound = false

function CreateRegion3FromPart(Part)
	return Region3.new(Part.Position - (Part.Size / 2), Part.Position + (Part.Size / 2))
end

function GetPlayersInPart(Part)
	local Region = CreateRegion3FromPart(Part)
	local PartsInRegion = game.Workspace:FindPartsInRegion3(Region, nil, math.huge)

	local Players = {}

	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
	
	local Teams = {}
	
	for i, team in pairs(game.Teams:GetChildren()) do
		Teams[team] = {}
		
		for i, value in pairs(team:GetPlayers()) do
			for i, player in pairs(Players) do
				if value == player then
					table.insert(Teams[team], player)
				end
			end
		end
	end
	
	print(repr(Teams, {pretty=true}))

	return Players
end

while task.wait() do
	GetPlayersInPart(game.Workspace.Hardpoint.PlayerDetector)
end

Which kinda works how I want it to, but it’s rather buggy- it also feels very botched.

Although this doesn’t directly answer your question, have you looked into Zone+?

It’s a pretty feature-rich module that allows you to configure and utilize 3d regions very easily, it also comes with playerEntered and playerExited events that you can hook into. You can also easily get a table of players within a region. (Documentation is listed under the thread above)

I only plan to have the one zone in my game, and to use this module just seems a bit too much.