A server-side way to check if a player is still connected

I have code that looks like the following:

foreach (var player in Players.ToList())
{
    try
    {
        // something asynchronous that could take several hundreds of ms to complete
    }
    catch
    {
    }
}

During that loop, a player could disconnect, because in the loop I trigger client events and delay until the client triggers a response server event.

I’m wondering why isn’t there something like player.IsConnected. It would help.

1 Like

Assuming in your case player is the serverId of the player, then you could use GetPlayerName(player), if it’s nil then your player is disconnected :slight_smile:

1 Like

thanks

1 Like

what if player is timeout ? or in re-connection , will this do the job ?

Hmmm, no idea try it, when i want to TriggerClientEvent on a specified client i check with GetPlayerName before and it does work.

Another thing would be to listen on the event playerDropped, that depends on your needs

2 Likes

well when a player is in re-connection playerDropped won’t call ! even tho will test it tomorrow

You can check the players ping. If it’s above 0, then assume they’re connected?

1 Like

player ping has a few seconds delay so not a solution

not works either when the player is re-connecting

Ping players manually using events. I suggest using TaskCompletionSource to make it awaitable. Something so trivially done in script doesn’t really need a feature request.