Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Output showing Table.remove() Problems?

Asked by 8 years ago

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

Log in to vote
0
Answered by 8 years ago

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"
0
So how will I remove a certain, randomized player? fight4money -2 — 8y
0
wrong. you can remove variables too davness 376 — 8y
0
I updated the answer. Try using that function. CodingEvolution 490 — 8y
0
No dav, you cannot remove a value from a dictionary using table.remove() alone. CodingEvolution 490 — 8y
View all comments (3 more)
0
Thank you so much! fight4money -2 — 8y
0
values you cant, variable you can davness 376 — 8y
0
I don't think we're on the same page here. Could you give an example of what you mean? CodingEvolution 490 — 8y
Ad

Answer this question