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

How do i delete a table array after a players leaves the game with the same name?

Asked by 4 years ago

if a player leaves (for example Player2), how do i make the table delete the array "Player2" after Player 2 leaves from the game?

i'm baffled and this is all i could do right now:

players = {}

local function onCharacterAdded(chr)
    table.insert(players, chr)
    local value = players[math.random(1,#players)]
    print(value)
end

local function onCharacterRemoved(removed)
    table.remove(players, removed)
end

game.Players.PlayerAdded:Connect(onCharacterAdded)
game.Players.PlayerRemoving:Connect(onCharacterRemoved)

while true do
    wait(2)
    print(players[1])
    print(players[2])
end

1 answer

Log in to vote
0
Answered by 4 years ago

table.remove only works if the index value is a number and not an instance type. So for this to work you'd have to find the index of that specific player and then use table.remove to remove the index in the table.

local index = -1

for i, v in pairs(players) do
    if removed == v then
        index = i
    end
end

table.remove(players, index)

This should find the index of the player removing and then remove that player from the table.

Ad

Answer this question