How to scale inventory templates ui with mainframe ui size


as seen in the images, the mainframe grows when theres more buttons inside of its container in a uigridlayout. how could i achieve this via scripting so that the mainframe will grow to fit the buttons inside if more are added and if less are added it should shrink. im not sure how to do this any advice would be greatly appreciated.

Add the appropriate height per row.

LocalScript --manual math
--LocalScript in StarterPlayerScripts
local mainFrame = script.Parent.MainFrame
local grid = mainFrame.UIGridLayout

local function updateMainFrameSize()
    local children = {}
    for _, child in ipairs(mainFrame:GetChildren()) do
        if child:IsA("TextButton") then
            table.insert(children, child)
        end
    end

    local buttonCount = #children
    local buttonsPerRow = math.floor(mainFrame.AbsoluteSize.X / grid.CellSize.X.Offset)
    buttonsPerRow = math.max(1, buttonsPerRow)
    
    local rows = math.ceil(buttonCount / buttonsPerRow)
    local totalHeight = rows * grid.CellSize.Y.Offset + (rows - 1) * grid.CellPadding.Y.Offset
    
    mainFrame.Size = UDim2.new(mainFrame.Size.X.Scale, mainFrame.Size.X.Offset, 0, totalHeight)
end

grid:GetPropertyChangedSignal("AbsoluteContentSize"):Connect(updateMainFrameSize)
updateMainFrameSize()
LocalScript --refined
--LocalScript in StarterPlayerScripts
local mainFrame = script.Parent.MainFrame
local grid = mainFrame.UIGridLayout

local function updateMainFrameSize()
    mainFrame.Size = UDim2.new(mainFrame.Size.X.Scale, mainFrame.Size.X.Offset, 0, grid.AbsoluteContentSize.Y)
end

grid:GetPropertyChangedSignal("AbsoluteContentSize"):Connect(updateMainFrameSize)
updateMainFrameSize()

No testing here.. so :flushed_face:

I don’t think you actually need scripting. It can just be done using AutomaticSize, which is one of the properties on gui objects or something. You can play around with UIGridLayouts and UIPadding as well.

2 Likes

i tried this and got results like this, it would never scale it correctly. the main background frame wouldnt grow larger than the container of buttons and i have no clue why

maybe some ui padding would help?

robloxdestroyer1035 has the real way. I’m trying to force it. Should just be set up to do that.

hive.rbxm (11.2 KB)
RobloxStudioBeta_ndWBjvF1l7

this is most likely the style you are going for, though i did not properly scale it