Removing a key from a table & Updating menu command.

I am currently developing this sort of derma menu friends list, and I’ve managed to make a add feature ( table.insert() ), but I’m not sure about a remove feature. The table.remove() only remove’s numbers from a table mildly confused, but alright. I hope there’s and alternative way to do it.

Also, with this menu when I use the add function it adds it to the friends list variable from the all players in server it adds the variable but the menu doesn’t update with the new variable showing the person that was added to the friends list. This also should be a very easy solution but I’m mildly new to lua.

Your help would be appreciated, thank you.

Wait what? You mean like
[lua]table[2] = nil[/lua]

Table.remove removes a key from your table, based on its place in the table.

So if you have a table of 5 values, and you table.remove(<table>, 3), you’ll remove the 3rd value in your table, as well as realign the other values to their correct positions within the table.

The values need to be realigned, because you just removed the 3rd entry, which would be blank otherwise.

Imagine your table like a stack of bricks. table.remove removes the brick at the given spot.

When you remove the brick at the given spot, you have an empty space now, so table.remove will also move the bricks above the one you removed, down into the spot you just cleared.

If it doesn’t make sure to do this, you would have an empty space at the spot you cleared, until you set it to another value again.

I’m thinking you don’t understand how tables completely work. Tables have a key and a value, table.insert automatically allocates the next possible key for the number of values already inserted, so if you inserted 3 values already, table.insert would make the key 4.

table.insert returns the key it inserted with thus you can use table.remove with that key as the second argument.

Can you paste your current code please.

Alright that makes sense now, so now if I want to remove a certain person’s name from the list, I have to determine what number they are in the table and then remove that key?

local Friends = { "Bob", "John", "Nick", "Tyler" }

concommand.Add( "+test_menu", function() 
    local DermaPanel = vgui.Create( "DFrame" ) 
    DermaPanel:SetPos( 50,50 ) 
    DermaPanel:SetSize( 500, 400 ) 
    DermaPanel:SetTitle( "Friends List" ) 
    DermaPanel:SetVisible( true )
    DermaPanel:SetDraggable( true ) 
    DermaPanel:ShowCloseButton( true ) 
    local PlayerListBox = vgui.Create( "DComboBox", DermaFrame )
    PlayerListBox:SetParent( DermaPanel )
    PlayerListBox:SetPos( 10, 35 )
    PlayerListBox:SetSize( 100, 185 )
    PlayerListBox:SetMultiple( false )
    for k,v in pairs(player.GetAll()) do
        PlayerListBox:AddItem( v:Nick() )
    local FriendsListBox = vgui.Create( "DComboBox", DermaFrame )
    FriendsListBox:SetParent( DermaPanel )
    FriendsListBox:SetPos( 200, 35 )
    FriendsListBox:SetSize( 100, 185 )
    FriendsListBox:SetMultiple( false ) 
    for k,v in pairs(Friends) do
        FriendsListBox:AddItem( v )
    local AddButton = vgui.Create( "DButton" )
    AddButton:SetParent( DermaPanel ) 
    AddButton:SetText( "Add >" )
    AddButton:SetPos( 125, 50 )
    AddButton:SetSize( 60, 25 )
    AddButton.DoClick = function ()
        if PlayerListBox:GetSelectedItems() and PlayerListBox:GetSelectedItems()[1] then
            table.insert(Friends, PlayerListBox:GetSelectedItems()[1]:GetValue())
            DermaPanel:MakePopup() -- I was hoping this would "update" the menu as in re-open it, but I don't think that works. (This is my second question on how to "update" the menu *re-send*).
    local RemoveButton = vgui.Create( "DButton" )
    RemoveButton:SetParent( DermaPanel )
    RemoveButton:SetText( "Remove <" )
    RemoveButton:SetPos( 125, 90 )
    RemoveButton:SetSize( 60, 25 )
    RemoveButton.DoClick = function ()
        <remove selection>
end )

Here it is, its probably not top notch but I’ve done my best. The “add” function works like I said but the “remove” function need to be fixed. If what I stated up above when replying to IceBreakers post is how it’ll need to work or unless there’s another function with table(s) that will accomplish the same task with less work.


RemoveButton.DoClick = function ()
local p = FriendsListBox:GetSelectedItems()[1]:GetValue()
table.remove(Friends, table.KeyFromValue§)

I think this’ll work for your RemoveButton.DoClick function.

However, you can never set custom Keys in the ‘Friends’ table, cause this function will break if it doesn’t get a numerical value equal to its position in the table.

Test it out like 20 times and see what happens with it. The more i think about it, the more i doubt it’ll work off the bat.

You should have a table with the key being the players userid and the value being a boolean to represent if they are currently a friend.