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

Remove player from table when player leaves?

So I was wondering how and if this was possible.

I was wondering how to remove a player from a table when they leave, or if it automatically removes them.

Please give a link to a tutorial if possible, or just tell me here!

2 answers

✓ Accepted answer

There is a Signal called PlayerRemoving of the Players Service. You can Connect a Listener to this to remove them. This Signal also returns the PlayerObject of the Player Leaving, so we can use this to our advantage for finding them in the array.

You can also you :GetPlayers of Players to create an array holding the Players. Though the actual Players Service (Folder) will remove the Player automatically, this table won't update as it will only hold the Players that were existant when called. Therefore you'd have to manually remove them.

local Players = game:GetService("Players")

local PlayerArray = Players:GetPlayers()

Players.PlayerRemoving:Connect(function(Player)
   for i = 1,#PlayerArray do
      if (Player == PlayerArray[i]) then
         table.remove(PlayerArray, i)
      end
   end
end)

2 comments

0

T-T

0

Changed my mind. Just realized this one is more helpful in my case.


Player Removing and GetPlayers


Couple of things here:

  • Number 1, There is a preexisting table of players in game that you can get via game.Players:GetPlayers()

  • Number 2, There is a PlayerRemoving event of the players service

So, with that said, you can do the following:

game.Players.PlayerRemoving:Connect(function(plr)
	local Plrs = game.Players:GetPlayers()
	...
end)

Hopefully this helped!

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