Support.AddUserInputListener("\069\110\100\101\100","\077\111\117\115\101\066\117\116\116\111\110\049",true,function(v)if not SnappingStage then return end FinishSnapping()end)function FinishSnapping()if not SnappingStage then return end if SnappingStartSelectionState then for v,U in pairs(SnappingStartSelectionState)do v:MakeJoints()v.CanCollide=U.CanCollide v.Anchored=U.Anchored end end SnappingStage=nil SnapTracking.StopTracking()if DirectionLine then DirectionLine:Destroy()DirectionLine=nil end if AlignmentLine then AlignmentLine:Destroy()AlignmentLine=nil end if HistoryRecord then RegisterChange()end ClearConnection("\083\110\097\112\068\114\097\103\083\116\097\114\116")ClearConnection("\083\110\097\112\068\114\097\103")ClearConnection("\083\110\097\112")ClearConnection("\083\110\097\112\068\114\097\103\069\110\100")end function GetFaceOffsetsFromCorner(v,U)local L={}local C=GetFacesFromCorner(v,U)for C,I in pairs(C)do local G=(Vector3.FromNormalId(I)*v.Size)/2 local y=(CFrame.new(U)*CFrame.Angles(v.CFrame:toEulerAnglesXYZ()))*G table.insert(L,{Face=I;Offset=y})end return L end function GetFacesFromCorner(v,U)local L={}for C,I in pairs(Enum.NormalId:GetEnumItems())do local G=v.CFrame*((v.Size/2)*Vector3.FromNormalId(I))local y=(U-G).magnitude table.insert(L,{Proximity=y,Face=I})end table.sort(L,function(v,U)return v.Proximity<U.Proximity end)return{L[1].Face;L[2].Face,L[3].Face}end
-- Stop snapping whenever mouse is released
Support.AddUserInputListener("Ended", "MouseButton1", true, function(Input)
	-- Ensure snapping is ongoing
	if not SnappingStage then
		return
	end

	-- Finish snapping
	FinishSnapping()
end)

function FinishSnapping()
	-- Cleans up and finalizes the snapping operation

	-- Ensure snapping is ongoing
	if not SnappingStage then
		return
	end

	-- Restore the selection's original state if stage was reached
	if SnappingStartSelectionState then
		for Part, State in pairs(SnappingStartSelectionState) do
			Part:MakeJoints()
			Part.CanCollide = State.CanCollide
			Part.Anchored = State.Anchored
		end
	end

	-- Disable any snapping stage
	SnappingStage = nil

	-- Stop snap point tracking
	SnapTracking.StopTracking()

	-- Clear any UI
	if DirectionLine then
		DirectionLine:Destroy()
		DirectionLine = nil
	end
	if AlignmentLine then
		AlignmentLine:Destroy()
		AlignmentLine = nil
	end

	-- Register any change
	if HistoryRecord then
		RegisterChange()
	end

	-- Disconnect snapping listeners
	ClearConnection("SnapDragStart")
	ClearConnection("SnapDrag")
	ClearConnection("Snap")
	ClearConnection("SnapDragEnd")
end

function GetFaceOffsetsFromCorner(Part, Point)
	-- Returns offsets of the given corner point in the direction of its intersecting faces

	local Offsets = {}

	-- Go through each face the corner intersects
	local Faces = GetFacesFromCorner(Part, Point)
	for _, Face in pairs(Faces) do
		-- Calculate the offset from the corner in the direction of the face
		local FaceOffset = (Vector3.FromNormalId(Face) * Part.Size) / 2
		local Offset = CFrame.new(Point) * CFrame.Angles(Part.CFrame:toEulerAnglesXYZ()) * FaceOffset
		table.insert(Offsets, { Face = Face, Offset = Offset })
	end

	-- Return the list of offsets
	return Offsets
end

function GetFacesFromCorner(Part, Point)
	-- Returns the 3 faces that the given corner point intersects

	local Faces = {}

	-- Get all the face centers of the part
	for _, FaceEnum in pairs(Enum.NormalId:GetEnumItems()) do
		local Face = Part.CFrame * (Part.Size / 2 * Vector3.FromNormalId(FaceEnum))

		-- Get the face's proximity to the point
		local Proximity = (Point - Face).magnitude

		-- Keep track of the proximity to the point
		table.insert(Faces, { Proximity = Proximity, Face = FaceEnum })
	end

	-- Find the closest faces to the point
	table.sort(Faces, function(A, B)
		return A.Proximity < B.Proximity
	end)

	-- Return the 3 closest faces
	return { Faces[1].Face, Faces[2].Face, Faces[3].Face }
end
