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)
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
.
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 Winner
s. (See here for implementation details)
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;