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

remove player from table?

Asked by 9 years ago

okay, I made this so when a the game assigns teams, the names are inserted into a table called "houseguests". My question is, how would i remove a certain player's name from the table?

function AssignTeam()
    local team = Instance.new("Team")
    team.Parent = game.Teams
    team.Name = "Houseguests"
    team.TeamColor = BrickColor.new(330)
    team.AutoAssignable = false
    local player = game.Players:GetPlayers()
    local players = game.Players:GetChildren()
    for i,v in pairs(player) do
        v.TeamColor = BrickColor.new(330)
    end
    for i,v in pairs(players) do
        table.insert(houseguests, v.Name)
    end
end

1 answer

Log in to vote
2
Answered by 9 years ago

Setting the index to nil

One of the most common ways of removing a value from a table is simply setting it's value to nil. This will completely erase the value's existence and recognition with the script, thus completely removing it. This is done via the following example:

local Array = {'hello','world'}

-- I gave a source below of a question about tables in which I explain arrays
-- This code simply indexed the table for the second value ('world'), and sets it to nil (removing it from the table)

Array[2] = nil -- "world" is now removed

-- Now if we try to print it...
print(Array[2]) -- "nil"

Clearing a table?

An extension of this example could be making a function that wipes all data in an array. Here's an example:

local tab = {"hello","i","am","steve"}

local function ClearTable(t)
    for i = 1,#t do
        t[i] = nil -- Set the current index to nil
    end
end

print(unpack(tab)) -- > "hello i am steve"

ClearTable(tab) -- run the function

print(unpack(tab)) -- "" (nothing)

table.remove

There's one handy function of the table library called remove. This function consists of 2 arguments:

  1. The table you're modifying
  2. The numeric key you're removing

Now, because we can only pass a number as the second argument of table.remove, this implies our table must be an array. If you don't know what an array is, I explain a bit more about it in this question: https://scriptinghelpers.org/questions/25964/what-are-tables#29555

Now, this function actually has an extra mechanic to it than just removing a value from a table. Once removing a value from a table with this function, it will actually replace that index value with the key that's after it. Here's an example:

local test = {"hello","i","am","steve"}

table.remove(test,2) -- remove "i" from the array

print(test[2]) -- this will print "am", since it was the next value after "i" (which was just removed)

So this function removes the value, with the addition of moving all other elements down the list, replacing the removed value. This is different from just manually setting the index to nil, where manually setting the index to nil leaves a blank space open as nil, where a value used to be. Here's an example demonstrating that:

local text = {"hello","world","i","like","pie"}

-- table.remove
table.remove(text,2)
print(text[2]) -- "i"

-- removing index
text[2] = nil
print(text[2]) -- "nil"

Your question

Applying all this information, here's a scenario which fits your question:

local players = {"player1","player2","player3","player4","player5"}

local function RemovePlayer(tab,playerName)
    for i = 1,#players do
        local v = players[i] -- get the current value
        if v == playerName then
            table.remove(tab,i) -- remove it
            -- or tab[i] = nil
        end
    end
end

RemovePlayer(players,"player2")

print(players[2]) -- > "player3" (since we used table.remove, it removed the second value in the table and pushed all the other elements down by 1 to fill the nil values place)

Hope this helped, let me know if you have any questions.

0
thank you so much! it did :D Marrleey 55 — 9y
Ad

Answer this question