Scaling multiple parts in a Build Game causes it to drift off

I am trying to make a build system for my game where the players can move parts, scale parts, and rotate parts on their plot but there’s an issue. When the player scales a single part it works fine, but it slightly drifts off when they select more than 2 parts. I do not know how to solve this issue, and the way it scales is correct, not proportional scaling yet.

Here is what’s happening:


And here is the script:

elseif placedType == "Scale" then
			local currentPos = UserInputService:GetMouseLocation()
			local mouseRay = camera:ViewportPointToRay(currentPos.X, currentPos.Y)
			local currentT = getLineClosestParam(mouseRay.Origin, mouseRay.Direction, axisOrigin, dragDir)
			local delta = currentT - startDragT
			local moveDistance = tonumber(editGui.Frame.Building.Move.Text) or 1
			local snapSteps = math.round(delta / moveDistance)
			local snappedDelta = snapSteps * moveDistance
			local minClamp = math.max(moveDistance, 0.05)
			for p, originalCF in pairs(initialPartCFrames) do
				if p and p.Parent then
					local origSize = initialPartSizes[p] or p.Size
					local deltaApplied = snappedDelta
					local newSize = origSize
					if selectedAxis == "X" then
						local desired = origSize.X + deltaApplied
						local clamped = math.max(desired, minClamp)
						deltaApplied = clamped - origSize.X
						newSize = Vector3.new(clamped, origSize.Y, origSize.Z)
					elseif selectedAxis == "Y" then
						local desired = origSize.Y + deltaApplied
						local clamped = math.max(desired, minClamp)
						deltaApplied = clamped - origSize.Y
						newSize = Vector3.new(origSize.X, clamped, origSize.Z)
					elseif selectedAxis == "Z" then
						local desired = origSize.Z + deltaApplied
						local clamped = math.max(desired, minClamp)
						deltaApplied = clamped - origSize.Z
						newSize = Vector3.new(origSize.X, origSize.Y, clamped)
					end
					if deltaApplied ~= 0 then
						p.Size = newSize
						p.CFrame = originalCF + (dragDir * (deltaApplied * 0.5))
					end
				end
			end
			updateGizmos()
		end

I have fixed that issue but now there’s a different issue in the new edited function. Whenever I select multiple parts and scale it, it works fine with no position issues, but let’s say I try to scale multiple of parts Upwards with a rotated part. The player will expect every part to scale upwards but that is not what is happening, it is scaling based on the part’s axis’
Here is the new function:

elseif placedType == "Scale" then
			local currentPos = UserInputService:GetMouseLocation()
			local mouseRay = camera:ViewportPointToRay(currentPos.X, currentPos.Y)
			local currentT = getLineClosestParam(mouseRay.Origin, mouseRay.Direction, axisOrigin, dragDir)
			local delta = currentT - startDragT
			local moveDistance = tonumber(editGui.Frame.Building.Move.Text) or 1
			local snapSteps = math.round(delta / moveDistance)
			local snappedDelta = snapSteps * moveDistance
			local minClamp = math.max(moveDistance, 0.05)
			for p, originalCF in pairs(initialPartCFrames) do
				if p and p.Parent then
					local origSize = initialPartSizes[p] or p.Size
					local deltaApplied = snappedDelta
					local newSize = origSize
					if selectedAxis == "X" then
						local clamped = math.max(origSize.X + deltaApplied, minClamp)
						deltaApplied = clamped - origSize.X
						newSize = Vector3.new(clamped, origSize.Y, origSize.Z)
					elseif selectedAxis == "Y" then
						local clamped = math.max(origSize.Y + deltaApplied, minClamp)
						deltaApplied = clamped - origSize.Y
						newSize = Vector3.new(origSize.X, clamped, origSize.Z)
					else
						local clamped = math.max(origSize.Z + deltaApplied, minClamp)
						deltaApplied = clamped - origSize.Z
						newSize = Vector3.new(origSize.X, origSize.Y, clamped)
					end
					if deltaApplied ~= 0 then
						p.Size = newSize
						local axisVec
						if selectedAxis == "X" then
							axisVec = originalCF.RightVector
						elseif selectedAxis == "Y" then
							axisVec = originalCF.UpVector
						else
							axisVec = originalCF.LookVector
						end
						if axisVec:Dot(dragDir) < 0 then
							axisVec = -axisVec
						end
						p.CFrame = originalCF + axisVec * (deltaApplied * 0.5)
					end
				end
			end
			updateGizmos()
		end

Here’s what’s happening:

I still haven’t found a solution to make the size of the parts all scale to the same direction no matter how much it’s rotated, well it should detect the closest rotation to the direction they are pointing at

You seem to be confusing X, Y, and Z of the positional axes in the workspace with the X, Y, and Z values for size of BaseParts. The Z value is always the size of the object in the direction of the lookVector.

I can’t think of an easy solution for your specific problem to address this discrepancy, but hopefully this still helps.