Alternative to table.remove()

Hey developers,

I’m not sure exactly how to word this but I will certainly try my best.

Lets say we have a Dictionary with my Username and a “Level”,

local dict = {
   ['wayIxn'] = 100,
}

I will be adding names in which I do not know so heres an example of it whenever I need to remove a value.

local dict = {
   ['wayIxn'] = 100,
   ['Telamon'] = 35,
   ['Roblox'] = 10, -- levels really have nothing to do with this.
}

Okay, I do not know what these values are.

I need to remove a string so I want to remove Roblox from the list. I do not know its place in the dictionary (3) therefore we cannot remove using table.remove(). I know no other way of removing this value.

Thanks! (Sorry, I had absolutely no clue how to word this feel free to give suggestions heh…)

1 Like

All that you are required to do is:

dict.Roblox = nil

Once you’ve done this, there will no longer be any value associated with the "Roblox" key, and it will not come up if you loop through the table.

12 Likes

Dictionaries do not have order. Just because you assigned the values in that order, does not mean that’s how they’ll appear in a loop. As Notwal said - setting the value of the key to nil removes it from the table.

You could set values of an array to nil to remove them, too… however this would leave gaps in your array. This is why table.remove exists, solely.

10 Likes

Wow, never actually thought of this… Happy I know it now, thank you!! :slight_smile:

1 Like

Awesome, thanks so much! I’ve been trying to figure out how to fix my bug all night.

1 Like