Region3 Not Working As Expected

I’m new to using Region3 and I don’t quite understand how to use it (due to the lack of documentations) . Could someone point out what’s wrong with this code:

local Part1 = workspace.Part1
local Part2 = workspace.Part2
local Part3 = workspace.Part3
local Part4 = workspace.Part4

while true do
   local Region = {
      Region3.new(Part1.Position,Part2.Position),
      Region3.new(Part3.Position,Part4.Position)
   }

   if workspace:IsRegion3Empty(Region[1]) == false or workspace:IsRegion3Empty(Region[2]) == false then
      print("Object Detected!")           
   end
   wait(0.1)
end
1 Like

Any errors?

I’d recommend using FindPartsInRegion3 instead of IsRegion3Empty, as it is much easier and more understandable. There is an abundance of documentation as well. You can also create a whitelist of what objects you want found in the Region3, using FindPartsInRegion3WithWhiteList.

1 Like

Nope, there are no errors in the output. The code however returns that there are no objects in the defined region when there are in fact objects inside the region.

I’d recommend reading the post I made above in that case.

local function SensorDetection()
	while true do
		local SensorRegion = {
			Region3.new(Sensor[1].Position,Sensor[2].Position),
			Region3.new(Sensor[3].Position,Sensor[4].Position)
		}
		local FrontSensorDetectedObjects = workspace:FindPartsInRegion3(SensorRegion[1])
		local RearSensorDetectedObjects = workspace:FindPartsInRegion3(SensorRegion[2])
		
		if #FrontSensorDetectedObjects > 0 then
			print("Objects Detected!")
		end
		wait(0.1)
	end
end

spawn(SensorDetection)

(Video Removed)

The area I’m jumping in is one of the defined regions, but I’m not getting anything in the output.

1 Like

I’m not quite sure what your problem is seeing as there is no image of the parts that create the Region3s in question, but I have found a few resources that can help you understand Region3

Scripting Helpers Link
YouTube Video
DevForum post that gives an alternative

edit: You’ve just posted a video showing it lol, are you sure you’re creating the Region3s along this format?
image

png
These are two parts that create the region.

1 Like

You just need two points, one on each corner of the Region3. Take this for example,
image

Edit beat me to it :stuck_out_tongue:

Yup, that’s the format I’m using, also, can the Vector3 value have a decimal point?

Example:

local VectorPosition1 = Vector3.new(0.1,0.1,0.1)
local VectorPosition2 = Vector3.new(5.1,5.1,5.1)

local Region = Region3.new(VectorPosition1,VectorPosition2)
1 Like

Oh I see. Maybe you could make sure that the region is correct by placing a part at the same place

local Part = Instance.new(“Part”)
Part.Size = SensorRegion[1].Size
Part.CFrame = SensorRegion[1].CFrame
Part.Anchored = true
Part.CanCollide = false
Part.Parent = workspace

I forgot to mention that the Region3s are meant to move around, is there something wrong with this setup?

1 Like

That can be really stressing performance wise.

1 Like

Do note that workspace:IsRegion3Empty has no blacklist parameter supplied, so it will detect the parts used to construct the Region.

Also note that the Region3 constructor has this annoying requirement that the positions passed to it must be min and max. See the below example.

local Part1 = workspace.Part1
local Part2 = workspace.Part2

local region1Upper = Vector3.new(
	math.max(Part1.Position.X, Part2.Position.X),
	math.max(Part1.Position.Y, Part2.Position.Y),
	math.max(Part1.Position.Z, Part2.Position.Z))

local region1Lower = Vector3.new(
	math.min(Part1.Position.X, Part2.Position.X),
	math.min(Part1.Position.Y, Part2.Position.Y),
	math.min(Part1.Position.Z, Part2.Position.Z))	

while wait(0.1) do
   local Region = Region3.new(region1Lower, region1Upper)

	for _, Part in pairs(game.Workspace:FindPartsInRegion3WithIgnoreList(Region, {Part1, Part2}, math.huge)) do
		print(Part.Name)
	end
end

https://developer.roblox.com/api-reference/datatype/Region3

6 Likes

If your Region3s are becoming rotated I’d check this out as I don’t believe Region3s can be rotated

Region3 works. Your implementation is most likely just finnicky or unintended. What you could do is try to create a pseudo part between the two points of your region to see if, at the very least, your region is being properly created and moved as intended. Once the region implementation is gold, then move on to the functionality portion.

If you’re still having trouble with this, do you mind creating a demo that demonstrates the erroneous behaviour, and then upload it here so we can open the place file and take a look?

Regions are one of those things where it’s really difficult to see the problem without it in front of you. My hunch is that your region ends up being flat (if your parts are on the same Y coordinate) but I’d need to see it to be sure.

1 Like

I was actually able to somewhat solve the issue. Instead of using Region3 and FindPartsInRegion3, I used a Part, .Touched event and :GetTouchingParts().

Previously:

local Part1 = workspace.Part1
local Part2 = workspace.Part2
local Part3 = workspace.Part3
local Part4 = workspace.Part4

while true do
   local Region = {
      Region3.new(Part1.Position,Part2.Position),
      Region3.new(Part3.Position,Part4.Position)
   }

   if workspace:IsRegion3Empty(Region[1]) == false or workspace:IsRegion3Empty(Region[2]) == false then
      print("Object Detected!")           
   end
   wait(0.1)
end

Now:

local Part = script.Parent

local function SensorTouched()
   local Parts = Part:GetTouchingParts()
   if #Parts > 0 then
      print("Objects Detected!")
   end
end

local function SensorTouchEnded()
   local Parts = Part:GetTouchingParts()
   if #Parts == 0 then
      print("Sensor Clear!")
   end
end

Part.Touched:Connect(SensorTouched)
Part.TouchEnded:Connect(SensorTouchEnded)

However, I’m not sure if this new code is optimal in a large scale.

It should be noted that GetTouchingParts will only return parts that are CanCollide (or else have a Touched connection). Your solution also requires a debounce due to how the Touched event can fire many times at once.

Using a sensor part however saves you needing to repeatedly check the region, as I don’t believe Region3 has something like an Entered event.

4 Likes