Changing a singlular UDim2 component

I have a surface GUI which I’m trying to resize the elements within as their added to take up the whole board. My issue is that in the second half of my script when I try to scale the Y components, it rewrites the X component to 0. I know I’ve written 0 in the script but I’m not sure how to preserve the X as what they were before. I need to do the X any Y separate. I tried doing going down to the CellSize.X.Offset but that doesn’t work as according to multiple questions on UDim2 you are not able to rewrite single components of UDim.

while true do
			local ingui = game.Workspace.ListOfPlayers.SurfaceGui:GetChildren()
			if #ingui == 2 then 
				game.Workspace.ListOfPlayers.SurfaceGui.UIGridLayout.CellSize = UDim2.new(0,game.Workspace.ListOfPlayers.SurfaceGui.CanvasSize.X,0,0)
			elseif #ingui == 3 then
				game.Workspace.ListOfPlayers.SurfaceGui.UIGridLayout.CellSize = UDim2.new(0,game.Workspace.ListOfPlayers.SurfaceGui.CanvasSize.X / 2,0,0)
			elseif #ingui == 4 then
				game.Workspace.ListOfPlayers.SurfaceGui.UIGridLayout.CellSize = UDim2.new(0,game.Workspace.ListOfPlayers.SurfaceGui.CanvasSize.X / 3,0,0)
			elseif #ingui >= 5 then
		game.Workspace.ListOfPlayers.SurfaceGui.UIGridLayout.CellSize = UDim2.new(0,game.Workspace.ListOfPlayers.SurfaceGui.CanvasSize.X / 4,0,0)
		
			end
			if #ingui>= 2 and #ingui <= 5 then 
				game.Workspace.ListOfPlayers.SurfaceGui.UIGridLayout.CellSize = UDim2.new(0,0,0,game.Workspace.ListOfPlayers.SurfaceGui.CanvasSize.Y)
			elseif #ingui>= 6 and #ingui <= 7 then
				game.Workspace.ListOfPlayers.SurfaceGui.UIGridLayout.CellSize = UDim2.new(0,0,0,game.Workspace.ListOfPlayers.SurfaceGui.CanvasSize.Y / 2)
			elseif #ingui>= 8 and #ingui <= 13 then
				game.Workspace.ListOfPlayers.SurfaceGui.UIGridLayout.CellSize = UDim2.new(0,0,0,game.Workspace.ListOfPlayers.SurfaceGui.CanvasSize.Y / 3)
			elseif #ingui>= 14 and #ingui <= 17 then
				game.Workspace.ListOfPlayers.SurfaceGui.UIGridLayout.CellSize = UDim2.new(0,0,0,game.Workspace.ListOfPlayers.SurfaceGui.CanvasSize.Y / 4)
			end
    ingui = {}
		wait(0.5)
end

How can I write is so that the X is preserved and not changed to 0?

You can access the X property of the element, for example:

ImageButton.Size.X.Scale

So, you can use this as a placeholder in the X position:

ImageButton.Size = UDim2.new(ImageButton.Size.X.Scale, 0, 1, 0)

It won’t change the X size as it keeps it up-to-date whenever you access it.

You can do this with the Y dimension and offset as well.