Player/Team Detecting Zone Help

Hello,

I’m working on a Flag Capture system, and I’m currently trying to detect when players enter a zone. This is so I can add them to dictionary under their teams name, so that I can work out how many players from each team are in the zone which would allow me to find out if the zone is contested.

Though it’s not working how I would like it- here’s my code:

local PlayerFound = false
local repr = require(3148021300)

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 Teams = {}
	
	for i, Team in pairs(game.Teams:GetChildren()) do
		Teams[Team] = {}
	end

	for i, Part in pairs(PartsInRegion) do
		local Player = game.Players:GetPlayerFromCharacter(Part.Parent)

		if Player then
			for i, v in pairs(Teams[Player.Team]) do
				if Teams[Player.Team][i].Name == Player.Name  then
					PlayerFound = true
				end
			end

			if PlayerFound == false then
				table.insert(Teams[Player.Team],Player)
			end
			
			wait(1)
			print(repr(Teams, {pretty=true}))

			PlayerFound = false
		end
	end

	return Teams
end

while task.wait() do
	GetPlayersInPart(game.Workspace.Hardpoint.PlayerDetector)
end
  • Player enters zone
  • If player is already in table then end
  • Else add to table under teams name

Then when the player leaves the zone they should be removed from the table, though it doesn’t seem to be removing them, or account for more than one person.

If you’re able to understand it, and can tell me where I have made a mistake I’d appreciate it- feel free to try it in studio.

Thanks

1 Like

I highly suggest using ZonePlus by ForeverHD, it greatly simplifies the process of creating and handling zones, with this module, you can use the Zone.playerEntered event, I hope this helps!

I would, but the module just seems very cluttered and a bit much for what I am trying to do. Thank you for replying though!

-- This is a way I do zones.
	for i,v in pairs(zones) do
		coroutine.wrap(function()
		while wait(0.5) do
			local whitelist = {}
			for i,v in pairs(ps:GetPlayers()) do
				if v.Character then
					whitelist[#whitelist+1] = v.Character
				end
			end
			for a,b in pairs(workspace:FindPartsInRegion3WithWhiteList(v.Region,whitelist)) do
				if isPlayer(b.Parent.Name) then
					local p = uService.stringToPlayer(b.Parent.Name)
					if PlayerEnteredZones[p.UserId][i] == true then else
						PlayerEnteredZones[p.UserId][i] = true
						coroutine.wrap(function() v.OnPlayerEnter(p) end)()
					end
				end
			end
			end
		end)()
	end
1 Like