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 check if a player has left the server?

How would I check if a player has left the server?

2 comments

0

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

0

Please do.

3 answers

✓ Accepted answer

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

0

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.

0

I edited the code. I assume it's within a called function, so I added a disconnection to it, too.

0

What's the point in disconnecting?

0

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

0

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.