Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Output showing Table.remove() Problems?

I tried to remove a player with a variable of alien from a table called playersin as shown:

table.remove(playersin, alien)

(The Rest of the Script works perfectly fine)

The output shows me this:

16:58:55.598 - ServerScriptService.Game.G:148: bad argument #2 to 'remove' (number expected, got Object)

What does this mean?

1 answer

✓ Accepted answer

The table.remove function only removes values from a list. It will not search through the table looking for the value to match the second argument. It looks for the key, which is suppose to be a number.

Example:

local list = {
	"pie",
	"ice cream",
	"candy",
}

table.remove(list, 2) -- this will remove "ice cream" because it's index value is 2.

If you want to make it so you remove a value from a table based on it's value, you can write a separate function that compares every value in the table to an argument you give it.

Like this:

local list = {
	"pie",
	"ice cream",
	"candy",
}

local function remove_value(Table, Value)
	for i,v in pairs (Table) do
		if v==Value then
			table.remove(Table,i)
		end
	end
end

remove_value(list, "pie")

print(unpack(list)) -- > "ice cream" , "candy"

7 comments

0

So how will I remove a certain, randomized player?

0

wrong. you can remove variables too

0

I updated the answer. Try using that function.

0

No dav, you cannot remove a value from a dictionary using table.remove() alone.

0

Thank you so much!

0

values you cant, variable you can

0

I don't think we're on the same page here. Could you give an example of what you mean?

This discussion is preserved as it was. New answers, comments, and votes are closed.