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 3 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
3 years ago

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

1game.Players.PlayerRemoving:Connect(function(player)
2    local index = table.find(playersAlive, player)
3    if (index) then
4        table.remove(playersAlive, index)
5    end
6end)

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

01local playersAlive = {}
02 
03while (true) do
04    wait(1)
05    -- do round stuff
06end
07 
08game.Players.PlayerRemoving:Connect(function(player)
09    local index = table.find(playersAlive, player)
10    if (index) then
11        table.remove(playersAlive, index)
12    end
13end)

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 — 3y
Ad

Answer this question