How would I check if a player has left the server?
How do I check if a player has left the server?
3 answers
The PlayerRemoving event of Players fires when a player leaves and passes the player who is leaving. The trick is, it fires just before the player actually gets removed from the game.
local connection
connection = game.Players.PlayerRemoving:connect(function(player)
if player == p1 then
-- your code (if player is p1)
connection:disconnect()
elseif player == p2 then
-- your code (if player is p2)
connection:disconnect() -- These prevent connections from stacking on top of each other and causing very bad lag.
end
end)
EDIT: Edited code to match explanation in comments.
4 comments
I have a round script, and the players names are p1 and p2. I was trying to check if one of those players have left.
I edited the code. I assume it's within a called function, so I added a disconnection to it, too.
What's the point in disconnecting?
Disconnecting stops it from having supreme lag when it's connected a couple hundred times.
game.Players.PlayerRemoved:connect(function(player)
print(.. player.Name .. " has left the server")
end)
This will tell you if someone has left and print their name in the output.
You would check if the player is nil, for example:
game.Players.PlayerAdded:connect(function(player)
wait()
if player == nil then
local h = Instance.new("Hint", game.Workspace)
h.Text = player.Name.." has left the server!"
wait(2)
h:Destroy()
end
end)
1 comment
This will not tell you when a player leaves a server. This only works if the player leaves the second it joins.
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
You could check by using the 'PlayerRemoving' event, if you want me to provide an Explanation, just let me know. :)
Please do.