How to clamp an object to a certain position

So recently, I have been trying to clamp and object relative to another object’s position but I am stumped. Basically, in the video, I noticed that when I drag one part near the other irregular parts position, it immediately clamps so that they both align. How would I create a similar system to this?

Note that this system is also used in f3x to make the clamping more smooth.

It looks to me as if it checks whether the center point of the part is within the other part. If that is the case, it then shifts it up by otherPart.Size.Y/2 + thisPart.Size.Y/2.

You could, whenever thisPart's position is changed, check workspace:GetPartsInPart(thisPart) to see which parts are in it. If there are any, check if thisPart.Position (aka the middle point) is within otherPart's bounds.

Ok but how would I make sure that it only clamps relative to that part?

Well, that happens by itself more or less.

-- Pseudo code:
this part position changed: {
	Does it intersect other parts? {
		Is this position within the bounds of other part? {
			set position of this part to:
			   Vector3.new(this.X, otherPart.Y/2 + this.Y/2, this.Z)
		} else {
			do nothing.
		}
	} else {
		do nothing
	}
}

As you can see, all calculations of this part’s position are made from this and that other part’s (the one you are interested in) position. In short, the part will only clamp to the part of which it’s midpoint was inside of.
I hope this makes sense.