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

Remove variable from table?

Asked by 9 years ago

Basically, I have a table and two variables.

game.ServerStorage["Give Items"].Event:connect(function()
players = {}
Assassin = ""
Protector = ""

And I have it so it gets the players.

function GetPlayers()
    for i,v in pairs(game.Players:GetChildren()) do
        table.insert(players,v)
    end
end

So, then, I continue to get the Assassin and the Protector.

function SelectPlayer()
    local num = math.random(1,#players)
    Assassin = players[num].Name
    table.remove(players, Assassin)
    print('Assassin is: ' ..Assassin)
end

And the table.remove won't remove him from the table, it said it expected a number, not a string. Help?

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

The parameter to table.remove is where the thing to remove is, not what it is.

So, we need to make a simple "find" function which will find Assassin and then remove the thing at that position.

function tableFind(tab,val)
    for k,v in pairs(tab) do
        if v == val then
            return k;
        end
    end
end

...


table.remove(players, tableFind(players, Assassin) );

We could also wrap it into a function if you wanted:

function tableRemoveValue(tab,val)
    return table.remove( tab, tableFind(tab, val) );
end

...

tableRemoveValue(players, Assassin);
0
It still said it calling for a number. LennyTheLucario 45 — 9y
Ad

Answer this question