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

[table] table.remove problem?

Asked by 9 years ago

My error : bad argument #2 to 'remove' (number expected, got table)

Settings = {
    Ranked = { -- 3=Owner, 2=Admin, 1=Member, 0=nil, -.5=Muted, -1=Banned
        {Name = "Player",Rank = 3};
        {Name = "Idiot",Rank = -.5};
    },
}

for _,v in pairs(Settings.Ranked) do
    local msg = msg:lower()
    local sub = msg:sub(7)      
    local data = v.Name:lower()
    local num = tonumber(-.5)
    if msg == 'all' and v.Rank == num or data:find(sub) and v.Rank == num then
        table.remove(Settings.Ranked, v) -- Error here
    end
end 
0
I tryed v[1] but it wouldnt remove the correct ban. MessorAdmin 598 — 9y

1 answer

Log in to vote
2
Answered by
noliCAIKS 210 Moderation Voter
9 years ago

v refers to the rank data. _ refers to the key/index. The simple solution to that would be replacing table.remove(Settings.Ranked, v) by table.remove(Settings.Ranked, _), but that messes everything up because it's in the middle of a loop. Instead, if you don't want any holes inside the table indices (it may be better to use the rank info as the key itself instead and simply set the value to true, but it may be a bit late for that now), you could try doing this:

local temp = {}
for _,v in pairs(Settings.Ranked) do
    local msg = msg:lower()
    local sub = msg:sub(7)      
    local data = v.Name:lower()
    local num = tonumber(-.5)
    if msg == 'all' and v.Rank == num or data:find(sub) and v.Rank == num then
        temp[#temp + 1] = _
    end
end
for i,v in ipairs(temp) do
    table.remove(Settings.Ranked, v + 1 - i) -- `+ 1 - i` is to accomodate for the removed indices.
end
Ad

Answer this question