# How would I find the distance between 2 parts?

I want to know how many studs there is between two parts. The reason why I can’t just use `.Magnitude` is because that will return the distance from two exact CFrames, and not the distance between the parts.

Lets say that the blue in the image is the Position property, and the circle thing is a part called “Test”. Even though “Test” is touching the part below, it still won’t say 0, as it is not touching the Position property Vector3 of the part below.

``````local function getSurfaceDistance(part1, part2)
local part1Size, part1Pos = part1.Size, part1.Position
local part2Size, part2Pos = part2.Size, part2.Position

local part1Min = part1Pos - part1Size / 2
local part1Max = part1Pos + part1Size / 2
local part2Min = part2Pos - part2Size / 2
local part2Max = part2Pos + part2Size / 2

local overlapX = math.max(0, math.max(part1Min.X - part2Max.X, part2Min.X - part1Max.X))
local overlapY = math.max(0, math.max(part1Min.Y - part2Max.Y, part2Min.Y - part1Max.Y))
local overlapZ = math.max(0, math.max(part1Min.Z - part2Max.Z, part2Min.Z - part1Max.Z))

return math.sqrt(overlapX^2 + overlapY^2 + overlapZ^2)
end

-- example usage
local part1 = game.Workspace.Part1
local part2 = game.Workspace.Part2

print("Surface distance between the parts is:", getSurfaceDistance(part1, part2))
``````

should work fine

Yeah that works, is it hard to make it say a negative number if it’s inside of the part?

Nope, its super simple actually.

Why negative distance? just make it return 0.

You can actually do it in three lines.

``````local part1 = workspace.Part1
local part2 = workspace.Part2
local distance = (part1.Position - part2.Position).Magnitude
``````

That doesnt make any sense.

Magnitude is the distance between two points, otherwise being the distance between two locations of seperate parts. A CFrame is a set of data that contains a Position, Rotation and Matricies.
For any whole numbers that are within said distance are the equivilent of one stud in which you would have your answer, so if you want a whole number, you are better off using `math.floor` for that purpose. It will always be positive because of how exponents are handled mathmatically, in which they cancel out any negative numbers:

``````-2 x 5 = -10
-2 x -2 = 4
-2^2 = -2 x -2 = 4
-- while this may return "-4", the number is surroudned by parenthesis
-- which would look like this: (-2)^2
-- Otherwise the negative will be applied after the exponent.
``````

If you get a negative distance, then there is something wrong with your code.
The Number will also never be completely 0 unless you are in the exact same position, otherwise it will always have a decimal/integer as a value.

If you are looking where two parts intersect, you are better off raycasting for that, as that will tell you where exactly they will intersect and the relevant data from said raycast. Its the easiest way to do this.

Read what he said. chr limit ww

1 Like

The reason why I want it to return a negative number if it is inside the part, is that I use this for a snapToGrid like this: `local snappedY = math.floor(position.Y / self.GridUnit + 0.5) * self.GridUnit + y_offset/2 - module:getSurfaceDistance(self.Item.PrimaryPart, self.Plot)`, but this will only work if it is above the part, so if it’s under, it will not snap to the part under.

I did, but it’s senseless tho.