Help with Panel

Hi, i want to create a inventory with 30 Slots. I have created a simple Panel with some slots:

Screenshot 2021-07-05 193028
Code:

local slotsbgpanel = INV_MENU.Menu:Add("DPanel")
    slotsbgpanel:SetSize( inv:GetWide() * .275, inv:GetTall() * .5)
    slotsbgpanel:SetPos( inv:GetWide() * .365, inv:GetTall() * .45 )
    slotsbgpanel:SetText("")
    slotsbgpanel.Paint = function(me,w,h)
        draw.RoundedBox( 0, 0, 0, w, h, Color(35,35,35,200))
    end
    local ypos = 0
    local xpos = 0
    for k, v in pairs(inv_slots) do
        local inv_slots_panel = vgui.Create("DButton", slotsbgpanel)
        inv_slots_panel:SetPos(xpos, ypos)
        inv_slots_panel:SetSize(slotsbgpanel:GetWide() * .15, slotsbgpanel:GetTall() * .15)
        inv_slots_panel:SetText("")
        inv_slots_panel.Paint = function( me, w, h)
            if not me:IsHovered() then
                draw.RoundedBox( 0, 0, 0, w, h, Color(185,180,175,200))
            else
                draw.RoundedBox( 0, 0, 0, w, h, Color(115,210,255,100))
            end
        end
        xpos = xpos + inv_slots_panel:GetWide() * 1.125
        ypos = ypos + inv_slots_panel:GetTall() * 0
    end

My question is how I can make it so that the panels are not always created to the left but after 6 panels they are created one line further down.

And if there is a smarter way to create the Panels with a for k, v in pairs because the inv_slots code looks like this:

local inv_slots = {
    1,
    2,
    3,
    4,
    5,
    6,
}

thx

It depends how you want them to be indexed. You can use a single for loop to generate a table of slots. As for the panels, you can use the division (a / b) and modulo (a % b) operators to go from a index (like 8) to a position (2, 2).