I have a table of parts, how would I get their average CFrame?
module.inputSelectedObjectReturnTrueOrNil ← my table
module.numberSelected ← length of table (#table)
This is what I’ve come up with, with the help of AI:
local function returnCenterCFrameOfAllSelectedParts():CFrame
local avgPos = Vector3.zero
local qx, qy, qz, qw = 0, 0, 0, 0
for part, _ in module.inputSelectedObjectReturnTrueOrNil do
avgPos += part.Position
local _, _, _, x, y, z, w = part.CFrame:GetComponents()
-- Ensure quaternions face the same direction to avoid flipping
if qw ~= 0 and (x*qx + y*qy + z*qz + w*qw) < 0 then
x, y, z, w = -x, -y, -z, -w
end
qx += x
qy += y
qz += z
qw += w
end
-- Normalize quaternion
local length = math.sqrt(qx*qx + qy*qy + qz*qz + qw*qw)
if length > 0 then
qx, qy, qz, qw = qx/length, qy/length, qz/length, qw/length
else
qx, qy, qz, qw = 0, 0, 0, 1
end
--
avgPos /= module.numberSelected
return CFrame.new(avgPos.X, avgPos.Y, avgPos.Z, qx, qy, qz, qw)
end
Currently this doesn’t work because w in the line with part.CFrame:GetComponents() is R00, according to this website: GetComponents
Any quaternion experts, please come to the rescue! (if quaternions are even needed)
And of course, thank you all in advance ![]()



