Please post code with proper formatting
local RegionPart = script.Parent
local VisibleRegion = true
local RegionTransparency = 0.8
local NewPart = Instance.new("Part", workspace)
NewPart.Anchored = true
NewPart.Size = Vector3.new(1, 1, 1)
NewPart.CFrame = CFrame.new(RegionPart.Position + Vector3.new(-RegionPart.Size.X / 2, RegionPart.Size.Y / 2, -RegionPart.Size.Z / 2))
local NewPart2 = Instance.new("Part", workspace)
NewPart2.Anchored = true
NewPart2.Size = Vector3.new(1, 1, 1)
NewPart2.CFrame = CFrame.new(RegionPart.Position + Vector3.new(RegionPart.Size.X / 2, -RegionPart.Size.Y / 2, RegionPart.Size.Z / 2))
local Region = Region3.new(
NewPart.Position,
NewPart2.Position
)
if VisibleRegion then
VisualizePart = Instance.new("Part", workspace)
VisualizePart.Size = Region.Size
VisualizePart.CFrame = Region.CFrame
VisualizePart.Anchored = true
VisualizePart.CanCollide = false
VisualizePart.Transparency = RegionTransparency
end
while true do
wait(2)
print(#workspace:FindPartsInRegion3WithIgnoreList(Region, {RegionPart, NewPart, NewPart2, VisualizePart}, 100))
end
The problem is here:
NewPart.CFrame = CFrame.new(RegionPart.Position + Vector3.new(-RegionPart.Size.X / 2, RegionPart.Size.Y / 2, -RegionPart.Size.Z / 2))
and here:
NewPart2.CFrame = CFrame.new(RegionPart.Position + Vector3.new(RegionPart.Size.X / 2, -RegionPart.Size.Y / 2, RegionPart.Size.Z / 2))
You’re missing a (-
) on RegionPart.Size.Y / 2
in the first CFrame, and there’s one that shouldn’t be there at -RegionPart.Size.Y / 2
in the second CFrame.
Every component of the first Vector3 passed to Region3.new must be lower than the corresponding component of the second Vector3.
There’s also a simpler way of doing what you’re doing detailed here
It’s great that you found a solution on your own though, it’s a much better lesson than just copying some snippet off the wiki . Just wanted to let you know there’s a simpler way
- A Part’s Size is always positive in every component, it doesn’t make sense to have negative size (I’ve no idea what happens if you try tho ). That means the X, Y and Z of a Part’s Size are always positive.
-
The Region3.new constructor expects two positions, representing two opposing corners of the constructed Region3. But not any two opposing corners, it has to be first the lower, leftmost, front corner and then the upper, rightmost, back corner. Otherwise it’ll create a Region3 with negative volume, and it’s impossible for any parts to be inside of it and honestly it should probably throw an error when we try to do this
-
You were defining the Region3 corners like this:
local lowerCorner = RegionPart.Position + Vector3.new(-RegionPart.Size.X / 2, RegionPart.Size.Y / 2, -RegionPart.Size.Z / 2)
local upperCorner = RegionPart.Position + Vector3.new(RegionPart.Size.X / 2, -RegionPart.Size.Y / 2, RegionPart.Size.Z / 2)
When defining lowerCorner
:
When you’re adding the negative X and Z components of the Size, you’re making the X and Z parts of the corner lower because of (1). Since this is for the first corner, that’s good.
When you’re adding the positive Y component of the Size, you’re making it bigger. This is bad because this is for the first corner where every component needs to be smaller than for the second corner.
When defining upperCorner
:
You’re making the Y component of the corner smaller by subtracting the Y part of the size.
- To fix that, you just flip the signs on the Y components of each corner.