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?

01function AssignTeam()
02    local team = Instance.new("Team")
03    team.Parent = game.Teams
04    team.Name = "Houseguests"
05    team.TeamColor = BrickColor.new(330)
06    team.AutoAssignable = false
07    local player = game.Players:GetPlayers()
08    local players = game.Players:GetChildren()
09    for i,v in pairs(player) do
10        v.TeamColor = BrickColor.new(330)
11    end
12    for i,v in pairs(players) do
13        table.insert(houseguests, v.Name)
14    end
15end

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:

1local Array = {'hello','world'}
2 
3-- I gave a source below of a question about tables in which I explain arrays
4-- This code simply indexed the table for the second value ('world'), and sets it to nil (removing it from the table)
5 
6Array[2] = nil -- "world" is now removed
7 
8-- Now if we try to print it...
9print(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:

01local tab = {"hello","i","am","steve"}
02 
03local function ClearTable(t)
04    for i = 1,#t do
05        t[i] = nil -- Set the current index to nil
06    end
07end
08 
09print(unpack(tab)) -- > "hello i am steve"
10 
11ClearTable(tab) -- run the function
12 
13print(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:

1local test = {"hello","i","am","steve"}
2 
3table.remove(test,2) -- remove "i" from the array
4 
5print(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:

1local text = {"hello","world","i","like","pie"}
2 
3-- table.remove
4table.remove(text,2)
5print(text[2]) -- "i"
6 
7-- removing index
8text[2] = nil
9print(text[2]) -- "nil"

Your question

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

01local players = {"player1","player2","player3","player4","player5"}
02 
03local function RemovePlayer(tab,playerName)
04    for i = 1,#players do
05        local v = players[i] -- get the current value
06        if v == playerName then
07            table.remove(tab,i) -- remove it
08            -- or tab[i] = nil
09        end
10    end
11end
12 
13RemovePlayer(players,"player2")
14 
15print(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