How to automate the function, IMPORTANT?

I’ll be brief, I’m making an infinity Craft type game in roblox: https://neal.fun/infinite-craft/

Maybe someone knows)

And now I’m making an inventory with player texts:

And I have a problem that every time there is a new object it does not move down, and I want it to move down and they all stand evenly, I made a check for this:

if newObject.Position.X.Scale >= 0.92 then
		newObject.Position = UDim2.new(totalLength - 0.14, 0, totalHeight + 0.06, 0)
		print(totalLength, totalHeight)
	end

But as you can see it only works for one lane, and if there are new objects they won’t move, so I’d like to automate it somehow so that when an object realises that it has moved too far down x, it will go down y, and if there is already an object there, it will go even lower, but I don’t know how to do it. Help please, if anything, here is a part of the code that is responsible for creating the text and positioning it:

local function CreateGUIObject(objectName)
	local newObject = CreateBeautifulText(objectName)
	
	-- Find all existing text label objects in the GUI
	local existingLabels = gui:GetChildren()
	
	local totalHeight = 0.01
	for _, label in pairs(existingLabels) do
		if label:IsA("TextLabel") then
			-- Add the current size of the object to totalHeight
			totalHeight = totalHeight + label.Size.Y.Scale - 0.0393
		end
	end

	
	local totalLength = 0.77
	for _, label in pairs(existingLabels) do
		if label:IsA("TextLabel") then
			totalLength = totalLength + label.Size.X.Scale + 0.01
		end
	end
	newObject.Position = UDim2.new(totalLength, 0, totalHeight, 0)
	
	if newObject.Position.X.Scale >= 0.92 then
		newObject.Position = UDim2.new(totalLength - 0.14, 0, totalHeight + 0.06, 0)
		print(totalLength, totalHeight)
	end

end

Just use either UiListLayouts or UiGridLayouts (i recommend UiGridLayouts) for organising UI objects

can you tell me more about it and show me how I should do it in the script? If u don’t mind)

They are instances. You add into the frame where you hold the Buttons

You should use UIGridlayouts for this. Here is a tutorial