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

Is there a way to remove a person from a Table when they die?

Asked by 8 years ago

I have the connect function for dying here, I just can't figure out how I would go about removing someone from a table when they died. Is this even possible or do I have to go about it differently?

Here is the code:

WinnerTable = {Name, Name2, Name3}

game:GetService('Players').PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
            table.remove(WinnerTable, player)
        end)
    end)

2 answers

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

table.remove removes something at a particular point. It does not remove a thing in the list.

You have to find where player is inside WinnerTable to be able to table.remove it.

for i = 1, #WinnerTable do
    if WinnerTable[i] == player then
        table.remove(WinnerTable, player)
    end
end

Remark: The above only works if you are sure something is in the list only once. (In this case you should be good as long as you don't have any bugs elsehwhere!) If it's something's in it multiple times, potentially they won't all be deleted [Ex.: Why?]. All that's needed to fix that is to go backwards: for i = #list, 1, -1 do.


Alternative?

There is another way to do this, though (if you want to -- I'm not arguing one way is better than the other).

Right now, you know that player is still alive because they are in the WinnerTable. You could define it differently; for instance, they could have a Winner BoolValue in their player. When they start the game, the value is set to true. When they die, the value is set to false.

You no longer have a WinnerTable, then. Instead, if you need a list of players, you just look for those that are Winners. (See here for implementation details)

0
That's what I did for my mini game, game. ScriptFusion 210 — 8y
0
Thanks BlueTaslem! You've been really helpful for getting my coding along. megamario640 50 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

You could make your own function, RemoveFromTable, like this:


local function RemoveFromTable(Table, Value) for I = 1, #Table do local TableValue = Table[I]; if TableValue == Value then table.remove(Table, I); end end end RemoveFromTable(WinnerTable, Player)

Or you could just:


WinnerTable[Player] = nil;

Answer this question