Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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 comment

1

Thank you very much!

This discussion is preserved as it was. New answers, comments, and votes are closed.