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

How do you check the games players to see if a certain player has left?

I'm trying to find a way to check if a player (who's name is stored inside a StringValue) has left the game.

Please help!

local PlayerName = backup.Main.Owner

game.Players.PlayerRemoving:Connect(function(player)
	if not player:FindFirstChild.PlayerName -- Somehow replace 'PlayerName'
    print("A player has left: " .. player.Name)
end)

6 comments

0

Maybe make a RemoteFunction that the client calls in a loop to report to the server that it's still connected. Keep a record of when the last time the player called that function was. When a player leaves, check how long ago their last response on that RemoteFunction was.

0

@ABK2017 Why, the player(on PlayerRemoving event) is the player object(player.Name is the player name)

0

ABK2017 your answer makes no sense

0

and it is not helpful.

0

Also it would not be efficient at all

0

No it would not be so efficient...but if the time of the RF exceeded the loop time, clearly that player has left the game, that's all I meant.

4 answers

✓ Accepted answer

Hello, meonlyababy!

local PlayerName = backup.Main.Owner

game.Players.PlayerRemoving:Connect(function(player)
   print(player.Name) -- This will print the name of the player that disconnected
    -- IDK what you want, but 'player.Name' is the player that disconnected =D
    if player.Name == PlayerName.Value then
        print("The owner of this part has disconnected - " .. player.Name)
    end
end)

Good Luck with your games

3 comments

0

It doesn't seem to print the player's name after they leave. Any reason why?

0

The valie... it tests on line 6

0

For some reason I can't get it to output anything

Just get the name of the player leaving and the name inside of the StringValue. Here is what I mean:

local stringName = game.Workspace.StringValue -- I don't know where your string value is or what it is named. 
local players = game:GetService("Players") -- GetService is the recommended way to get services like the Players service
-- Create the event listener
players.PlayerRemoving:Connect(function(player)
	if player.Name == stringName.Value then --Remember to get the Value of the string Value here I get the players name and if it matches with what is stored inside the string value we proceed. put your code in here
	end
end)

Hope this helps and have a great day scripting!

To make code run when a player leaves the game, you can use the PlayerRemoving event. To make it limited to a specific player, you can simply add an if statement to check if the player's name matches that of a string value. Here is an example:

local StringValue = game.Workspace.StringValue

game.Players.PlayerRemoving:Connect(function(Player)
    if Player.Name == StringValue.Value then
        -- Insert code here.
    end
end)

I would recommend storing the value inside of your script to prevent exploiting, but for this example all you would have to do is change the StringValue variable to the directory of your value. (If your value is in the workspace, you should only have to change the directory to the name of your string value.) Hope this helped, good luck!

You didn't put then and end to the statement.

3 comments

2

This is not an answer nor does it assist in the problem at hand.

1

I really dislike ill informed answers. You know, this should be a comment, or delete this.

0

I agree with incapaz.

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