Region
Types
RegionInstance
The 3D representation of the Region's boundaries. It can either be a single BasePart or a Model composed of several BaseParts that all represent one Region.
Properties
name
Region.name:
string
The name of the Region. This is used when calling tostring()
on the Region instance.
(Default: "Region")
instance
Region.instance:
RegionInstance
A reference to the regionInstance
argument pass when constructing
entered
Region.entered:
RBXScriptSignal
Fired when an instance in the whitelist enters the Region.
local regionInstance = Instance.new("Part")
local region = Region.new(regionInstance)
region.entered:Connect(function(instance: Instance)
print(instance, "entered the region")
end)
left
Region.left:
RBXScriptSignal
Fired when an instance in the whitelist leaves the Region.
Usage:
local regionInstance = Instance.new("Part")
local region = Region.new(regionInstance)
region.left:Connect(function(instance: Instance)
print(instance, "left the region")
end)
Functions
new
Constructs a new Region instance.
You can either pass in a Model or a BasePart which will represent the Region's boundaries in the Workspace.
When using a Model, it must contain BaseParts as children.
A Region's BaseParts are known as "segments." Every Region must have at least one segment, and when a segment is collided with by instances in the whitelist certain events will be triggered.
local regionInstance = Instance.new("Part")
local region = Region.new(regionInstance)
is
Region.
is
(
other:Â
any
) →Â
boolean
--
Returns true
if other
is a Region, false
otherwise
Static function for checking if the given argument is a Region instance or not.
local regionInstance = Instance.new("Part")
local region = Region.new(regionInstance)
print(Region.is(region)) -- true
print(Region.is("string") -- false
setWhitelist
Sets the instances that can collide with the Region and trigger events.
The whitelist must be defined or the Region will not respond to any instances.
tip
A common use case is to set the whitelist to all the Character models in the Workspace. This allows you to setup a region system where your players can enter and leave specific zones.
local regionInstance = Instance.new("Part")
local region = Region.new(regionInstance)
region:setWhitelist({
Players.LocalPlayer.Character
})
getRegionSegments
Get all of the BaseParts that compose the Region's boundary.
info
If you passed in a BasePart when constructing, this will return an array with that BasePart as the only element. If you passed a Model, this will return an array with all BasePart descendants.
local regionInstance = Instance.new("Part")
local region = Region.new(regionInstance)
print(region:getRegionSegments())
getInstancesInRegion
Region:
getInstancesInRegion
(
) →Â
{
Instance
}
--
Array of whitelisted Instances that are within the Region
Gets all whitelisted Instances that are within the Region.
tip
The below example uses a while loop to print out all whitelisted instances
within the Region for illustrative purposes. Usually you will want to
respond to the enter
and left
events.
local regionInstance = Instance.new("Part")
local region = Region.new(regionInstance)
region:setWhitelist({
Players.LocalPlayer.Character
})
region:listen()
while task.wait(1) do
print(region:getInstancesInRegion())
end
isInstanceInRegion
Region:
isInstanceInRegion
(
instance:Â
Instance
) →Â
boolean
--
Returns true
if the Instance is within the Region, false
otherwise
Checks if the given Instance is within the Region.
This only applies to instances in the whitelist. If you call this function
on an instance that is not whitelisted, it will always return false
.
local regionInstance = Instance.new("Part")
local region = Region.new(regionInstance)
region:setWhitelist({
workspace.Part
})
region:listen()
while task.wait(1) do
print(region:isInstanceInRegion(workspace.Part))
end
listen
Region:
listen
(
) →Â
(
)
Starts a Heartbeat connection to listen for instances in the whitelist colliding with the Region.
This method must be called before instances in the whitelist will be detected within the Region's boundaries.
local regionInstance = Instance.new("Part")
local region = Region.new(regionInstance)
region:setWhitelist({
Players.LocalPlayer.Character
})
region:listen()
destroy
Region:
destroy
(
) →Â
(
)
Destroys the Region, cleaning up any connections and destroying Instances that the Region relied on.
Note that this will destroy regionInstance
.
local regionInstance = Instance.new("Part")
local region = Region.new(regionInstance)
region:setWhitelist({
workspace.Part
})
region:listen()
-- Later...
region:destroy()
print(regionInstance.Parent) -- nil