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

How would you get this to remove a string value from a table?

Asked by 9 years ago

print(script.Name..' loaded') local players= game.Players local surviving= { } roundActive= false intermission= true game.Players.PlayerAdded:connect(function(player) table.insert(surviving, player.Name) print(player.Name..' has joined the game') print(table.concat(surviving, ', ')) player.CharacterAdded:connect(function(character) character.Humanoid.Died:connect(function() table.remove(surviving, player.Name) print(player.Name..' has died') end) end) end) game.Players.PlayerRemoving:connect(function(player) if surviving:findFirstChild(player.Name) then table.remove(surviving, player.Name) else end print(player.Name..' has left') end) function getNewList() local plr= players:GetChildren() for i= 1,#plr do if surviving:findFirstChild(plr[i].Name) then print(plr[i].Name..' is already in the roster') else table.insert(surviving, plr[i].Name) print(plr[i].Name..' added to the roster') end end end function announceSurvivors() print(table.concat(surviving, ', ')) end

2 answers

Log in to vote
3
Answered by
woodengop 1134 Moderation Voter
9 years ago

The FindFirstChild method does not work with arrays(tables).

for key,value in pairs(surviving) do -- the pairs function is not necessary. 
    if player.Name==value then
        --  code
    end
end
Ad
Log in to vote
1
Answered by 9 years ago

The table.remove function removes the value at the index given. To remove a specific value you can loop through the table.

function removeValue(tab, val)
    for o, n in pairs(tab) do
        if val == n then
            table.remove(tab, o)
        end
    end
end
removeValue(surviving, player.Name)

Answer this question