Removing item from table

Basically i got a random value from a table with

local c = minigames[math.random(1, #minigames)]

And after in the script, i need to delete that item from the table. But to remove it, table.remove() requires the Index/Table position of that item and there’s no way i could know what that is, given it’s a random element from the table.

How do i do it?

1 Like
table.remove(minigames,table.find(minigames,c))

Easy way to remove instances, not really recomendend for other values

@Valkyrop explains it in detail
|
|

1 Like
local tab = {1,2,3,4}
local random = math.random(1,#tab)

table.remove(tab,table.find(random))

This should work.
Lets say you get “2”.

Table.find will look for that value and remove it.

@kalabgs gave you the exact solution for your case. I gave you a general idea.

Notice, the following won’t necessarily give you the number you got:

local tab = {1,2,5,4}
local random = math.random(1,#tab)

table.remove(tab,tab[random])

Assume we get here “5”, doing tab[5] will NOT remove the value “5” but the 5th index. In this case, it will return nil. Since there is no 5th index.

1 Like