Advanced derma stuff

Hello, so I am working on a script and I am trying out some different stuff with derma. I figured out a pretty cool way to implement a delete feature to the item I am working on, shown here…

The bottom stuff are buttons that I just haven’t gotten around to painting yet.

One of them is a delete feature, as shown here…

Here is the code for the delete feature…


   -- Looping through darkrp laws
            for k, v in pairs(darkrpLaws) do

                addNewLine = false
                local lawText = DarkRP.textWrap(tostring(k)..". "..v, "lawFont_20", laws.width - 5)

                -- Checking for '
' in the string
                if (type(string.find(lawText, "
")) == type(1)) then
                    addNewLine = true
                end

                local remove = vgui.Create("DImageButton", Laws)
                remove:SetText("")
                remove:SetPos(laws.offset - 2, laws.height/10 + count)
                remove:SetSize(16, 16)
                remove:SetImage("icon16/cross.png")
                
                remove.Paint = function(self, w, h)

                    surface.SetDrawColor(color)
                    surface.DrawRect(0, 0, w, h)

                end
                
                remove.DoClick = function(self)

                    RunConsoleCommand("say", "/removelaw "..k)
                    remove:SetSize(0, 0)

                end

                if addNewLine then
                    count = count + 22
                end

                -- Using a count for the line placement of the laws
                count = count + 22

            end

As you can see, I loop through the laws and create a button over where the number used to be, this works pretty well, but the only problem I have is, say I delete law 4 with this method…

The way I have it written is to basically just set the size of the button to zero if its clicked, DButton:Close() isn’t a thing for w/e reason I guess? Now the simple fix to my issue is to just delete the last button that was created in the loop instead of deleting the actual button I clicked which is what I am doing currently… How can I delete the last button in the loop??

I think the best way is to create a table for all buttons and just remove the last one.

This should work:
[lua]
local buttons = {} // Create the table
for k, v in pairs(darkrpLaws) do

            addNewLine = false
            local lawText = DarkRP.textWrap(tostring(k)..". "..v, "lawFont_20", laws.width - 5)

            -- Checking for '

’ in the string
if (type(string.find(lawText, "
")) == type(1)) then
addNewLine = true
end

            local remove = vgui.Create("DImageButton", Laws)
            remove:SetText("")
            remove:SetPos(laws.offset - 2, laws.height/10 + count)
            remove:SetSize(16, 16)
            remove:SetImage("icon16/cross.png")
            
            remove.Paint = function(self, w, h)

                surface.SetDrawColor(color)
                surface.DrawRect(0, 0, w, h)

            end

            table.insert(buttons,remove) // Add the button to the table
             
            remove.DoClick = function(self)

                RunConsoleCommand("say", "/removelaw "..k)
                buttons[#buttons]:Remove() // Remove the last button

            end

            if addNewLine then
                count = count + 22
            end

            -- Using a count for the line placement of the laws
            count = count + 22

        end

[/lua]

Thanks, although there is an issue, the table.insert is updated everytime before the remove function so the #buttons stays the same.