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