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!
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!
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)
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.
2 comments
T-T
Changed my mind. Just realized this one is more helpful in my case.