Is it possible to remove a value from a table without knowing the position of the value?

Salutations !
I have a script that inserts a value into a table when one of multiple GUI’s is clicked.

local Table = {}

for i,v in pairs (GUI:GetChildren()) do
	if (v:IsA("ImageButton")) then
		v.MouseButton1Click:Connect(function()
			table.insert(Table, v.TextLabel.Text)	
		end)
	end
end

This script does its job well and works.

However, at some point in time, I will need to remove a value that was inserted in the table when another button is clicked.

My question is, how would I do that ? I only know the Value that I need to remove, but not the position of it.

Is there a :FindFirstChild() like function for tables ? Or should I keep track of the position the value is inserted in when it is inserted initially ?

Any help would be appreciated!

1 Like

use

for _,v in pairs(Table) do
if v=value you need then
...........
end
end

edit: or use dictionary

Table  = {}
Table[yourvalue]=true

and get it back by

Table[yourvalue]
1 Like

No - this is not possible.

You must know the position - What I’d recommend you do is use a dictionary.

example:

Table[v.TextLabel.Text] = true

Then you delete it by:

Table[v.TextLabel.Text] = nil;
13 Likes

There isn’t any inbuilt way to get the key of an item in a table using its value, but for your specific use case (where you’re using an array rather than a dictionary), @youshould_playobby 's solution will work fine.

For the sake of people searching for this later, if you did need to get the position of an item in a table from its value, you have two options:

If the table is unlikely to get very big, or you’re unlikely to search for items often, you can iterate through the entire table (like @mistr88 did) to find the entry with the value you’re looking for.

However, if performance is a concern and iterating through the entire table is impractical, you can create an “inverted” table, where the key and values are swapped. For example, if you’re storing the number of points each item of fruit is worth in a dictionary:

local Table = {
"Apple" : 100,
"Orange" : 50,
"Banana" : 20}

You’d create an inverted dictionary alongside it, like this:

local InvertedTable = {
100 : "Apple",
30 : "Orange",
20 : "Banana"}

If you then needed to get the item of fruit that was worth 100 points, you could access it via InvertedTable[100].

4 Likes

Although you have marked the solution, there actually is a way of tackling this issue. It is not a built-in ROBLOX function, however you can create a simple function to find the location of a piece of data located in a table.

local function FindPlace(Table,Data)
for i,v in pairs(Table) do
if v == Data then
return i
end
end
end

local Table = {}

for i,v in pairs (GUI:GetChildren()) do
if (v:IsA(“ImageButton”)) then
v.MouseButton1Click:Connect(function()
table.insert(Table, v.TextLabel.Text)
end)
end
end

Then, in the event that you would like to remove the data from the table, you would simply use:

table.remove(Table,FindPlace(Table,TextLabel))
– Substitute TextLabel for whatever the object is.

Essentially, what the function does, is it goes through every element in the table, and when the specified element is found, it returns the place (number) of the element.

I hope this helped!

You might as well use ipairs here because that’s faster and follows numerical indices. This code also assumes that the indices of an iterated table are numerical; if they aren’t, table.remove will throw.

There is no need for all this overhead when you can simply use a dictionary.

2 Likes

Thanks for catching that mistake.

I know this is 3 years late, but this is actually incorrect.
You can use:

table.remove(tableName, table.find(tableName, value))
24 Likes