Remove Values from the array using Instances

So, I have been trying to make it so that if you state the instance in the “table.remove” it gets removed. But it does not work, it says it has to be an index in the array. How can I make it so instead of stating the index in the “table.remove(array, 2)” I can do “table.remove(array, Food)”?

script:

local Food = game.Workspace.Food
local array = {Food, 5.4352, game.Workspace.ColorLooping.Parent}

table.remove(array, 1) --I want this to remove the instance I name
--My wanted version: table.remove(array, Food)
print(array)
1 Like

I don’t really get what you mean. You have to use the index to remove an item from an array.

local food = table.find(array, foodItem)
table.remove(array, food)

You need to state the the index in the “table.remove” to remove a value. But I want to state the instance itself: table.remove(array, Food) instead of table.remove(array, 1).

Here is the error I get while trying to do this:

ServerScriptService.Script:28: invalid argument #2 to 'remove' (number expected, got Instance)

“Number expected, got Instance”

I fixed it! You need to declare a variable!

Correct script:

local Food = game.Workspace.Food
local array = {Food, 5.4352, game.Workspace.ColorLooping.Parent}

Food = table.find(array, Food)
table.remove(array, Food)

print(array)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.