Adapt UIGridLayout CellSize compared to CellCount

Hello :wave:

I am trying to improve my hatch system to hatch many eggs at same time something like Pet Simulator 99.

I am blocked to how change size and position of cells in my ui to match the number of eggs.

I tried something like this but is not the good way I think.

local frame = script.Parent
local uigridlayout = script.Parent.UIGridLayout

local function calculate()
local cellCount = #frame:GetChildren() - 3 
local cellSize = math.min(100, math.floor(frame.AbsoluteSize.X / math.ceil(math.sqrt(cellCount))))
uigridlayout.CellSize = UDim2.new(0, cellSize, 0, cellSize)
end

for i=1,20  do
	Instance.new("Frame", frame)
	calculate()
	wait(0.1)
end 

It is most likely because you have some kind of constraint on them, a modifier on them. Send your explorer.

I can’t right now, I send you when I am able.

Just my explorer is only

Frame
   UIConstraint
   UIGrid
   Localscript
   Template

Sorry for late response.

Here my explorer:
image

And here the complete script:

local frame = script.Parent -- 
local uigridlayout = script.Parent.UIGridLayout

local function calculate()
	local cellCount = #frame:GetChildren() - 3 
	local cellSize = math.min(100, math.floor(frame.AbsoluteSize.X / math.ceil(math.sqrt(cellCount))))
	uigridlayout.CellSize = UDim2.new(0, cellSize, 0, cellSize)
end


local player = game.Players.LocalPlayer
local char = player.Character


for i=1,99  do
	Instance.new("Frame", frame)
	calculate()
end 

Here 99 result

Here 3 result