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

How Do I Remove a Player From a Table When They Leave the Game?

Asked by 2 years ago

Hello, I have a game where a player is put inside a "playersAlive" table. The only problem is that when they leave the game, the game itself doesn't recognize that they have left, and the round will not reset even when everyone else has died.

I was wondering how I could remove a player from the "playersAlive" table when they leave the game. Thanks in advance!

1 answer

Log in to vote
3
Answered by
Rinpix 639 Moderation Voter
2 years ago

Create an event listener to detect when a player leaves the game.

game.Players.PlayerRemoving:Connect(function(player)
    local index = table.find(playersAlive, player)
    if (index) then
        table.remove(playersAlive, index)
    end
end)

Make sure you define your playersAlive variable at the highest scope possible; it should look something like this:

local playersAlive = {}

while (true) do
    wait(1)
    -- do round stuff
end

game.Players.PlayerRemoving:Connect(function(player)
    local index = table.find(playersAlive, player)
    if (index) then
        table.remove(playersAlive, index)
    end
end)

If you defined the variable inside of the while loop, the event listener at the bottom of the script wouldn't recognize the variable.

1
Thank you very much! RealSillykids 3 — 2y
Ad

Answer this question