I know how to check if a player enters a region but can't figure out when they leave it.
01 | local Players = { } -- players in region gets stored into this table |
02 | local AreaPart = workspace.Area |
03 |
04 | function createRegion 3 FromPart(part) |
05 | local Size = part.Size |
06 | local Position = part.Position |
07 | return Region 3. new(Position-(Size/ 2 ), Position+(Size/ 2 )) |
08 | end |
09 |
10 | function getPlayersInPart(part) |
11 | if part and part:IsA( "BasePart" ) then |
12 | local newRegion = createRegion 3 FromPart(part) |
13 | local partsInRegion = workspace:FindPartsInRegion 3 (newRegion, nil , math.huge ) |
14 | for i, v in pairs (partsInRegion) do |
15 | if game.Players:FindFirstChild(v.Parent.Name) then |
I have created a second table which gets reset every time. This was we can check if all players that where in the area a couple of minutes ago, still are here.
01 | function getPlayersInPart(part) |
02 | if part and part:IsA( "BasePart" ) then |
03 | local newRegion = createRegion 3 FromPart(part) |
04 | local partsInRegion = workspace:FindPartsInRegion 3 (newRegion, nil , math.huge ) |
05 | local tempList = { } --this list resets each time |
06 | for i, v in pairs (partsInRegion) do |
07 | if game.Players:FindFirstChild(v.Parent.Name) then |
08 | local player = game.Players [ v.Parent.Name ] |
09 | table.insert(tempList, player) |
10 | if not Players [ player ] then |
11 | table.insert(Players, player) |
12 | end |
13 | end |
14 | end |
15 | for k,v in pairs (tempList) do --loop over every player found in this single check |
I hope this helps! Message me if you encounter errors.