How to remove items from this table?

Hey there!

I know this should seem really simple but I am not getting it, I know the basics of tables, but this is a different kind of table… I got this from a tutorial so bear with me.

This table

_G.boughtArray = {}

can be used for any player by calling it like this

local bought = _G.boughtArray[player.userId]

Instead of using table.insert, it uses this to add items to the table

_G.boughtArray[player.userId][Item] = Item

So I am a little unsure how to take items back out. Here is my code block

for i, ITEM in pairs(_G.boughtArray[player.userId]) do
	print(ITEM)
	print(_G.boughtArray[player.userId])
	print(_G.boughtArray[player.userId][ITEM])
	table.remove(_G.boughtArray[player.userId], _G.boughtArray[player.userId][ITEM])
end

the 3 print lines gave

  1. the current item

  2. the whole table

  3. the current item

but, when I tried to remove the item, I got

What I want to know, how can I take out the item, and how can I reference it using a number?

1 Like

Will this work?

	_G.boughtArray[player.userId][ITEM] = nil
3 Likes

The “remove” isn’t working because you are passing a string value into the brackets. Using .remove allows you to remove an item from a table by index, so Lua is expecting you to give it two indexes rather than an index and a string value.

@RobloxGamerPro200007 has a good solution to this problem, by setting the value of the item in the table to nil.

Have a look at this Stack Overflow post, I think it relates to what you’re trying to accomplish.
Remove specific entry from Lua table - Stack Overflow

1 Like

Yes it worked! Thanks I now see how that works from a new angle.