Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Possible to check if a part went out of a Region?

Asked by 6 years ago

As the title suggests, i want to know if it is possible to detect if a part went out of a region.

0
I have updated my answer to help. alphawolvess 1784 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

To detect if a part has exited the Region3, you would first need to keep a record on what part(s) have entered a Region3, you can easily do this by keeping track of parts within a table.

First off, lets make a Region3, I will use a function the Roblox Wiki provides to do so:

function CreateRegion3FromLocAndSize(Position, Size)
    local SizeOffset = Size/2
    local Point1 = Position - SizeOffset
    local Point2 = Position + SizeOffset
    return Region3.new(Point1, Point2)
end

CreateRegion3FromLocAndSize(Vector3.new(0, 0, 0), Vector3.new(5, 5, 5)) -- Creates Region3 at position 0,0,0 with a size of 5, 5, 5

Now that we have a region3, we can check for parts inside of this region3 using FindPartsInRegion3 which will return a table of all parts within our Region3. For the first time this happens, we must record the table on the first run then compare the second run to know if any parts have exited. If the recorded table is not equal to the returned table from FindPartsInRegion3, something has left the Region3. This is represented by the code bellow:

function CreateRegion3FromLocAndSize(Position, Size) -- Useful code borrowed from wiki
    local SizeOffset = Size/2
    local Point1 = Position - SizeOffset
    local Point2 = Position + SizeOffset
    return Region3.new(Point1, Point2)
end

local R3 = CreateRegion3FromLocAndSize(Vector3.new(0, 0, 0), Vector3.new(5, 5, 5)) -- Creates Region3 at position 0,0,0 with a size of 5, 5, 5

local RecordedParts = {}
for _, Part in pairs(game.Workspace:FindPartsInRegion3(R3, nil, math.huge)) do
    table.insert(RecordedParts, Part)
end

while wait(1) do
    local ReturnedParts = game.Workspace:FindPartsInRegion3(R3, nil, math.huge) --Parts given by function
    local ReformattedReturned = {}

    for _, Part in pairs(ReturnedParts) do -- Makes an easy to compare index
        ReformattedReturned[Part.Name] = true
    end

    for _, Part in pairs(RecordedParts) do -- if something in Recordedparts is not in ReturnedParts, then it has left the region3
        if not ReformattedReturned[Part.Name] then
            print(Part.Name.." has left the Region3!")
            RecordedParts[Part.Name] = false -- Removes parts that have exited 
        end
    end

    for _, Part in pairs(ReturnedParts) do -- Keeps Entered parts updated
        RecordedParts[Part.Name] = true
    end
end

---edited: Sorry, I made a mistake but after getting my head straight, this may work.

Hope this helps!

0
So to make sure i understand, i would have to create 2 tables, one with the old data, and one with the new data, and compare those, but how do you compare tables? danielsury06 18 — 6y
0
Check the answer, I've added code that may help. alphawolvess 1784 — 6y
0
Thank you! this helped me solve the problem! danielsury06 18 — 6y
Ad

Answer this question