PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

FindPartsInRegion3

Returns an array of BaseParts in the given DataType/Region3.

This function takes an optional maxParts parameter (default 20) which limits the number of BaseParts that can be returned. Once this number has been reached, the search for BaseParts will stop. This means some BaseParts may not be returned even if they are within the DataType/Region3

The optional ignoreDescendentsInstance parameter can be used to specify a specific instance for whom itself and all of its descendants should be ignored by this function. This can be useful when, for example, looking to see if any BaseParts are inside a BasePart other than the BasePart itself.

local min = part.Position - (0.5 * part.Size)
local max = part.Position + (0.5 * part.Size)
local region = Region3.new(min, max)
local parts = workspace:FindPartsInRegion3(region, part) --  ignore part

Variants of this function exist with ignore-list and white-list functionality, Workspace/FindPartsInRegion3WithIgnoreList and Workspace/FindPartsInRegion3WithWhiteList.

If no BaseParts are found, an empty array will be returned.

How do Region3 checks work?

Checking if a part overlaps a DataType/Region3 is not a simple process. It actually is time consuming and complicated. Instead it checks if parts are roughly in the same area. When this function is called, it figures out which voxels contain the DataType/Region3. It then figures out which parts might be in those voxels. It does this by comparing the axis-aligned bounding box (sometimes called the AABB) of the part with the voxels. The axis-aligned bounding box can be seen in Roblox Studio when a part is selected.

This means that the area that is inspected by the function may be larger than the DataType/Region3. For this reason it is recommended to make sure that the DataType/Region3 is on the voxel grid. The best way to do this is by setting the coordinates of the DataType/Region3 to multiples of 4 (since voxels are 4 x 4 x 4 studs).

This method is a fairly quick and easy way to see if parts are in a general area. If a game needs to know if parts are exactly in an area, then BasePart/GetTouchingParts should be used. There is a higher cost to using BasePart/GetTouchingParts since a part is needed in the Workspace and the function takes more time to run.

Parameters

Name Type Default Description

The DataType/Region3 to be checked.

nil

An Instance to be ignored.

int
20

The maximum amount of BaseParts to be returned.

Returns

Return Type Summary

An array of BaseParts within the DataType/Region3.


Code Samples


Workspace:FindPartsInRegion3

This will print the names of any parts inside a square from 0,0,0 to 10,10,10:

local Point1 = Vector3.new(0,0,0)
local Point2 = Vector3.new(10,10,10)
local Region = Region3.new(Point1,Point2)
for _,Part in pairs(game.Workspace:FindPartsInRegion3(Region,nil,math.huge)) do
  print(Part.Name)
end