for _, ducks in pairs(User.Inventory.Ducks) do
if ducks.Tier == Tier then
if ducks.Rarity == Rarity then
table.remove(User.Inventory.Ducks, ducks)
UpdateGems:Fire(Player, DuckStats[Tier][Rarity].Sell)
end
end
end
Looking at other sites, it just says do table.remove(table, 2) or whatever, but I can’t do that, as there is no way of knowing where in the tables ‘ducks’ is.
1 Like
You’re ignoring the index with _.
for index, ducks in pairs(User.Inventory.Ducks) do
User.Inventory.Ducks[index] = nil -- Dictionary
table.remove(User.Inventory.Ducks, index) -- Array
1 Like
I wouldn’t use table.remove()
. That function causes all of the following array indices to be re-indexed every time you call it to remove an array entry. It is therefore much faster to just “compact/re-index” the table in a single(1) pass through ourselves. [source]
For example,
-- start the loop
for index, object in pairs(yourTable) do
-- if statements here
if object.Data == value then
-- here's the best way to remove the object from the table, ready?
yourTable[index] = nil
-- that's it!
end
end
How does this work? Well index
is the number at which the object in the table is located at, since the table is running in the same order. So by calling yourTable[index]
, you’re “selecting” the object at that number position, for example;
yourTable[1]
, this would be the first value on your array
And so setting that value to nil “removes” the value from the table in one concurrent loop, rather than repeating the loop for each time an object is removed.
Correct me if i’m wrong about avoiding table.remove()
, but this is the way I learned how to remove something from an array.
1 Like