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

How would I remove a value from a table?

Asked by 7 years ago

So i'm trying to make a script that detects when a player enters & leaves by putting their name in a table. Problem is i'm not sure if you can use table.remove() to remove a value,

    local players = {}
game.Players.ChildAdded:connect(function(plr)
    table.insert(players, ""..plr.Name)
end)

game.Players.ChildRemoved:Connect(function(plr)
    table.remove(players, ""..plr.Name)
end)

Any tips on how I can get it to remove the child's name off of the table when they leave?

1 answer

Log in to vote
0
Answered by 7 years ago

You can't remove an item from a table by its name, you can only do it by its position. What you could do is cycle through the table until you find the position that matches the player's name, and remove that position.

Here is what I mean:

local players = {}

game.Players.ChildAdded:connect(function(plr)
    table.insert(players, ""..plr.Name)
end)

game.Players.ChildRemoved:Connect(function(plr)
    table.remove(players, ""..plr.Name)
    for i=1, #players do
        if plr.Name == players[i] then
            table.remove(players, i)
        end
    end
end)
0
Use PlayerAdded in the future. unmiss 337 — 7y
Ad

Answer this question