Why is my region 3 offcenter?

Why is my region3 still detecting the player outside the desired box?

I have this script here

local regionPart = game.Workspace:WaitForChild("SuffocationArea")
local pos1 = regionPart.Position - (regionPart.Size /2)
local pos2 = regionPart.Position + (regionPart.Size /2)
local region = Region3.new(
	Vector3.new(
		math.min(pos1.X, pos2.X),
		math.min(pos1.Y, pos2.Y),
		math.min(pos1.Z, pos2.Z)
	),
	Vector3.new(
		math.max(pos1.X, pos2.X),
		math.max(pos1.Y, pos2.Y),
		math.max(pos1.Z, pos2.Z)
	)
)
local cansuffo = script.Parent.can_suffocate

while wait(1) do
	local partsInRegion = workspace:FindPartsInRegion3(region,nil,1000)
	for i, part in pairs(partsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") ~= nil then
			cansuffo.Value = false
			break
		else
			cansuffo.Value = true
			break
		end
	end
end

Perfect region3, But even if I leave the game.Workspace.SuffocationArea It still detects my player character!

(orange is the suffocationarea part)

1 Like

FindPartsInRegion3 deprecated. Try use GetPartBoundsInBox


Now it wont work even when your in the region3

Can you show your code using :GetPartBoundsInBox()?


local regionPart = game.Workspace:WaitForChild("SuffocationArea")
local pos1 = regionPart.Position - (regionPart.Size /2)
local pos2 = regionPart.Position + (regionPart.Size /2)
local region = Region3.new(
	Vector3.new(
		math.min(pos1.X, pos2.X),
		math.min(pos1.Y, pos2.Y),
		math.min(pos1.Z, pos2.Z)
	),
	Vector3.new(
		math.max(pos1.X, pos2.X),
		math.max(pos1.Y, pos2.Y),
		math.max(pos1.Z, pos2.Z)
	)
)
local cansuffo = script.Parent.can_suffocate

while wait(1) do
	local partsInRegion = workspace:GetPartBoundsInBox(region,nil,1000)
	for i, part in pairs(partsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") ~= nil then
			cansuffo.Value = false
			break
		else
			cansuffo.Value = true
			break
		end
	end
end

GetPartBoundsInBox accepts center of box(CFrame) as first argument, size of box(Vector3) as second, and OverlapParams as third(optional)

You should read how GetPartBoundsInBox works before simply replacing FindPartsInRegion3.

:GetPartBoundsInBox() uses these parameters:

  • CFrame (basically the position of the box and where it’s looking)
  • Size
  • OverlapParams

And to set up the OverlapParams you just have to do this:

local Params = OverlapParams.new()
Params.FilterType = Enum.RaycastFilterType. -- Exclude / Include
Params.FilterDescendantsInstances = {Instance}

Exclude or Include depends on whether you want to blacklist or whitelist the instance that you’re filtering and its descendants

1 Like

Please can you add the correct thing to the main script! please and thank you

Haven’t tested it but this should work

local regionPart = game.Workspace:WaitForChild("SuffocationArea")
local cansuffo = script.Parent.can_suffocate

while wait(1) do
	local Params = OverlapParams.new()
	Params.FilterType = Enum.RaycastFilterType.Include
	Params.FilterDescendantsInstances = {workspace}
	
	local PartBoundsInBox = workspace:GetPartBoundsInBox(regionPart.CFrame, regionPart.Size, Params)
	for i, part in pairs(PartBoundsInBox) do
		if part.Parent:FindFirstChild("Humanoid") ~= nil then
			cansuffo.Value = false
			break
		else
			cansuffo.Value = true
			break
		end
	end
end
1 Like

It still doesnt work for some reason! No errors in output or anything, Just still works when im not in the box

Edit: I got it working! Thanks for your help man! If you’re curious, Here is the working script


local regionPart = game.Workspace:WaitForChild("SuffocationArea")

local cansuffo = script.Parent.can_suffocate

local Params = OverlapParams.new()
Params.FilterType = Enum.RaycastFilterType.Include -- Exclude / Include
Params.FilterDescendantsInstances = {Instance}

while wait(1) do
		local region = regionPart
		local boxPosition = region.CFrame
		local boxSize = region.Size
		local parts = workspace:GetPartBoundsInBox(boxPosition, boxSize, nil)
		for _, part in pairs(parts) do
			if part.Parent.Name == game.Players.LocalPlayer.Name then
				cansuffo.Value = true
				break
			else
				cansuffo.Value = false
			end
		end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.