How to check if parts are within X amount of distance from another brick

Hey, I am having a bit of trouble figuring out how to make a script where it checks if there are parts within a certain radius of your tool.

local ignoreSurroundingsList = {"Torso", "Head", "LeftLeg", "RightLeg", "LeftArm", "RightArm"}

function checkSurroundings(plr)
	script.Parent.Handle.CanCollide = true
	local Point1 = Vector3.new(0,0,0)
	local Point2 = Vector3.new(10,10,10)
	local Region = Region3.new(Point1,Point2)
	for _,Part in pairs(script.Parent.Handle:GetTouchingParts(Region,nil,math.huge)) do
		for i,v in ipairs(ignoreSurroundingsList) do
        	if Part.Name:lower() ~= v:lower() then
 				print(Part.Name)
				script.Parent.Handle.CanCollide = false
				return true
			end
		end
	end
	script.Parent.Handle.CanCollide = false
	return false
end

The code above is what I have, but my issue is that the output (being printed out) is always Torso being within the radius. I believe this means that I’m doing something wrong where it’s checking to see if its white-listed in the “ignoreSurroundingsList”. What am I doing incorrect here? Why does it keep printing Torso?

Earlier I was using FindPartsInRegion3, but I couldnt get it to work. I think it’s because of my lack of understanding of how this is supposed to be used. Ideally, I wanted to do:

for _,Part in pairs(script.Parent.Handle:FindPartsInRegion3(Region,nil,math.huge)) do

However, I think I am limited to just to using Workspace if I use that.

Does anyone have an idea on how I can tackle this problem?

I don’t think GetTouchingParts takes values in so you’d be using FindPartsInRegion3 for this case, you can check out FindPartsInRegion3WithIgnoreList with an example which might better suit with what you’re looking for.

Alright.

Do I have to do:
workspace:FindPartsInRegion3WithIgnoreList(region3, ignoreList, maxParts)

Or can I use:
script.Parent.Handle:FindPartsInRegion3WithIgnoreList(region3, ignoreList, maxParts)

1 Like

You’ll want to use the first one, so if you want to get the regions for your tool you can do this:

local function checkSurroundings()
	local SearchRange = 10
	local IgnoreList = {} -- Ignore list takes in objects, so you want to add the model/instance rather than a string
	local SearchRegion = Region3.new(Vector3.new(Handle.Position.X-SearchRange,Handle.Position.Y-SearchRange,Handle.Position.Z-SearchRange), 
		Vector3.new(Handle.Position.X+SearchRange,Handle.Position.Y+SearchRange,Handle.Position.Z+SearchRange))
	local MaxParts = 20
	local results = workspace:FindPartsInRegion3WithIgnoreList(SearchRegion, IgnoreList, MaxParts)
	for _, parts in pairs(results) do
		print(parts)
	end
end
1 Like

I’d just use vector subtraction. Regions won’t give you a spherical hit-box.

(anotherbrick.p - part.p).Magnitude <= X

2 Likes