Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

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

Asked by
Relatch 550 Moderation Voter
10 years ago

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

0
You could check by using the 'PlayerRemoving' event, if you want me to provide an Explanation, just let me know. :) TheeDeathCaster 2368 — 10y
0
Please do. Relatch 550 — 10y

3 answers

Log in to vote
0
Answered by
Tkdriverx 514 Moderation Voter
10 years ago

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.

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. Relatch 550 — 10y
0
I edited the code. I assume it's within a called function, so I added a disconnection to it, too. Tkdriverx 514 — 10y
0
What's the point in disconnecting? Relatch 550 — 10y
0
Disconnecting stops it from having supreme lag when it's connected a couple hundred times. Tkdriverx 514 — 10y
Ad
Log in to vote
1
Answered by 10 years ago
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.

Log in to vote
-2
Answered by 10 years ago

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)
0
This will not tell you when a player leaves a server. This only works if the player leaves the second it joins. Tkdriverx 514 — 10y

Answer this question