Show only common variable help

Notice how in the center left, there’s Transform > CFrame > Position

When I select the part, it only shows the common position between the selected parts. How would I do that? I’ve started here:

local function updateSelectedCommonPositions()
	
	local key, value = next(selectedTable, nil)

	local posX = key.Position.X
	local posY = key.Position.Y
	local posZ = key.Position.Z
	
	for part in selectedTable do
		
		if part.Position.X == posX then
			?
		end
		if part.Position.Y == posY then

		end
		if part.Position.Z == posZ then

		end
		
	end
	
end

Thanks!

Oh, and just to clarify:

If two of the three positions match, I’d like it to return
position1, nil, position3

because I’ll be putting those numbers into a UI thingie, so that it can be displayed, just like in roblox studio :smiley:

I can have this really ghetto solution:

local function updateSelectedCommonPositions()

	local key, value = next(boxes, nil)
	
	if key == nil then -- if the table is empty
		PropertiesBarListPositionTextBoxes.XposButton.Text = " "
		PropertiesBarListPositionTextBoxes.YposButton.Text = " "
		PropertiesBarListPositionTextBoxes.ZposButton.Text = " "
		return
	end

	local posX = key.Position.X
	local posY = key.Position.Y
	local posZ = key.Position.Z

	local xNotNil = true
	local yNotNil = true
	local zNotNil = true

	for part in boxes do -- checks whether all the X positions match with each other, Y positions match with each other, and Z positions match with each other.

		if part.Position.X ~= posX then
			xNotNil = false
		end
		if part.Position.Y ~= posY then
			yNotNil = false
		end
		if part.Position.Z ~= posZ then
			zNotNil = false
		end

	end

	if xNotNil == true then
		PropertiesBarListPositionTextBoxes.XposButton.Text = posX
	else
		PropertiesBarListPositionTextBoxes.XposButton.Text = " "
	end
	if yNotNil == true then
		PropertiesBarListPositionTextBoxes.YposButton.Text = posY
	else
		PropertiesBarListPositionTextBoxes.YposButton.Text = " "
	end
	if zNotNil == true then
		PropertiesBarListPositionTextBoxes.ZposButton.Text = posZ
	else
		PropertiesBarListPositionTextBoxes.ZposButton.Text = " "
	end

end

I feel like this can be greatly improved

Check if all parts have the same CFrame, if not set to nil

local selectedParts = {}

local commonCFrame = selectedParts[1].CFrame -- first parts cframe
local commonSize = selectedParts[1].Size -- first parts size

for _,part in selectedParts do
	if part.CFrame ~= commonCFrame then
		commonCFrame = nil
	end
	if part.Size ~= commonSize then
		commonSize = nil
	end
end
  1. my table is a dictionary, not an array
    image

  2. I want it to show the common individual positions, so if the X coordinate is the same in all the parts of the table, it should display the X coordinate. I have it working here, but I think my code could be better.

Check if all parts have the same Position, if not set to nil

local selectedParts = {}

local posX, posY, posZ

for part, _ in selectedParts do
   posX, posY, posZ = part.Position.X, part.Position.Y, part.Position.Z
   break
end

for part, _ in selectedParts do
	 posX = posX == part.Position.X and posX or nil
     posY = posY == part.Position.Y and posY or nil
     posZ = posZ == part.Position.Z and posZ or nil
end

Woah, this is promising! Can you explain what “posX = posX == part.Position.X and posX or nil” does? Thanks!

Yes it’s a fancy ternary operation. It is shorthand for:

if posX == part.Position.X then
    posX = posX -- value stays the same, you wouldn't have to write this line
else
    posX = nil -- if a any part doesn't have the same value set posX to nil
end

Whenever you see and and or used like that it is a ternary operation. Let’s break it down.

local value = condition and firstOption or secondOption
  • value is the variable you are setting
  • condition is a condition that is either truthy or falsy (truthy means not false or nil, falsy means false or nil)
  • firstOption is what value will be set to if condition is truthy
  • secondOption is what value will be set to if condition is falsy

Example:

local character = Player.Character or Player.CharacterAdded:Wait()

-- if Player.Character exists then character will be set to Player.Character

-- if Player.Character is nil then character will be set to 
-- the return of Player.CharacterAdded:Wait() (which is the character)

If you wanted the secondOption to be false you might see it written as

local value = condition and firstOption

this is identical to:

local value = condition and firstOption or false

One thing to note is that firstOption must be truthy or value will always evaluate to secondOption regardless of whether the condition is truthy.

local value = condition and false or true -- this will always evaluate to 'true'

Writing this code will give you a blue underline in the editor with the warning:

The and-or expression always evaluates to the second alternative because the first alternative is nil; consider using if-then-else expression instead.

Thank you for this explanation! Really helped to clear things up :smiley:

One thing though, can you add the “PropertiesBarListPositionTextBoxes.Button.Text = pos” to your code above? Thanks!