Search Unity

Question How do I remove a player from a lobby when they close the window?

Discussion in 'Lobby' started by kadenjantz, Jun 26, 2023.

  1. kadenjantz


    Jan 30, 2021
    Hello, when a client closes their window, they are not leaving the lobby. This is a particularly big problem if they are the host, because then the host never transfers to anyone else.

    I tried to make the client leave like this:
    Code (CSharp):
    1.     void OnApplicationQuit() {
    2.         Debug.Log("QUIT");
    4.         Leave();
    5.     }
    7.     async void Leave() {
    8.         await LobbyService.Instance.RemovePlayerAsync(LobbyManager.Instance.joinedLobby.Id, AuthenticationService.Instance.PlayerId);
    9.     }
    The log did appear, but the client did not leave the lobby. This also didn't work:
    Code (CSharp):
    1.     void OnApplicationQuit() {
    2.         Debug.Log("QUIT");
    4.         LobbyService.Instance.RemovePlayerAsync(LobbyManager.Instance.joinedLobby.Id, AuthenticationService.Instance.PlayerId);
    5.     }
    I'm not sure what else to try. Any ideas would be much appreciated, thanks!
  2. Mj-Kkaya


    Oct 10, 2017
    Hi @kadenjantz ,
    You have to set "Disconnect Removal Time" variable to 10 second. 10 is minimum value.
    This properties is in the Dashboard=>Lobby =>Config page.
  3. earthcrosser


    Oct 13, 2010
    Looks like this is a pretty old thread but I also ran into this problem when testing project. I configured the lobby as suggested above with no effect.
    I came up with this solution after looking at the example lobby project that Unity provides.

    Objective: Ensure that when the application is attempting to quit, any player that has joined a lobby is properly removed before the application fully exits.


    1. Event Subscription:
      • During initialization (UnityGameServicesInitializer_OnInitialized method), we subscribe to the Application.wantsToQuit event. This event is triggered when the application is preparing to exit.
    2. Checking Lobby Status:
      • In the Application_wantsToQuit event handler, we evaluate if the player is currently in a lobby using the HasJoinedLobby() method.
      • If the player has not joined a lobby, the method will immediately allow the application to exit by returning true for canQuit.
      • If the player is in a lobby, we initiate the process to remove the player from the lobby by invoking the LeaveBeforeQuit coroutine.
    3. Player Removal Process:
      • The LeaveBeforeQuit coroutine initiates the RemovePlayerAsync method, which is responsible for the asynchronous task of removing the player from the lobby.
      • We then pause the coroutine execution and wait until the removal task is completed using the WaitUntil method.
      • Once the player is successfully removed, the Application.Quit() method is called again to exit the application.
    4. Asynchronous Player Removal:
      • The RemovePlayerAsync method uses Unity's LobbyService to perform the asynchronous operation of removing the player from the specified lobby.
      • If the removal is successful, we reset the joinedLobby to null.
      • In case of any exceptions or errors during this process, an error message is logged.
    Outcome: Through this approach, we ensure that any player in a lobby is gracefully removed before the application terminates. If the removal is successful, the application is allowed to quit, ensuring a clean exit.

    Code (CSharp):
    2. protected override void UnityGameServicesInitializer_OnInitialized()
    3.         {
    4.             base.UnityGameServicesInitializer_OnInitialized();
    5.             Application.wantsToQuit += Application_wantsToQuit;
    6.             StartCoroutine(HeartbeatCouroutine());
    7.         }
    8. private bool Application_wantsToQuit()
    9.         {
    10.             bool canQuit = !HasJoinedLobby();
    11.             if (HasJoinedLobby())
    12.             {
    13.                 StartCoroutine(LeaveBeforeQuit(joinedLobby.Id, AuthServiceHandler.UnityAuthorizationServicePlayerId));
    14.             }
    15.             return canQuit;
    16.         }
    17. IEnumerator LeaveBeforeQuit(string lobbyId, string playerId)
    18.         {
    19.             var task = RemovePlayerAsync(lobbyId, playerId);
    20.             yield return new WaitUntil(() => task.IsCompleted);
    21.             Application.Quit();
    22.         }
    23. private async Task RemovePlayerAsync(string lobbyId, string playerId)
    24.         {
    25.             if (!IsServiceReady) return;
    26.             try
    27.             {
    28.                 await LobbyService.Instance.RemovePlayerAsync(lobbyId, playerId);
    29.                 joinedLobby = null;
    30.             }
    31.             catch
    32.             {
    33.                 Debug.LogError("Failed to remove player");
    34.             }
    35.         }
    Last edited: Aug 18, 2023
  4. stickylab


    Mar 16, 2016

    where is the dashboard ?
  5. stickylab


    Mar 16, 2016
    where should i put that script ?

  6. qngnht


    Jan 24, 2022
    Dont know why they limit the Lobby too much, only Host can access things while Host is disconnected from a device couldn't detect the Quit,so nobody can not access that lobby any more that gave too many reconnect/disconnect issues ... on Steam lobby, it is very easy to handle.