Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Unity Multiplayer Photon Engine (PUN 2) trouble returning to lobby

Discussion in 'Multiplayer' started by ookumspookuminc, Oct 17, 2019.

  1. ookumspookuminc

    ookumspookuminc

    Joined:
    Jul 30, 2018
    Posts:
    6
    Hello all,

    I have started working with PUN 2 recently and I am having trouble finishing the gameplay loop.

    So far I can get the client connected to the photon network, create a room and have other players join it, and start the game with other players.

    The problem starts when I am trying to create a new room after leaving the game.
    When calling JoinOrCreateRoom I get this error

    JoinOrCreateRoom failed. Client is on GameServer (must be Master Server for matchmaking)but not ready for operations (State: Leaving). Wait for callback: OnJoinedLobby or OnConnectedToMaster.
    UnityEngine.Debug:LogError(Object)

    Here is a screenshot of my disconnect code
    https://imgur.com/a/0M9qKzm

    When I run the OnQuitMultiplayer function, the client begins to disconnect from the room, but never actually disconnects. because of this I can't create or join any other rooms without resetting the game.

    I'm not really sure what I am missing, any and all help is greatly appreciated
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,990
    Behind the scenes, there is a Master Server for matchmaking and Game Servers for the gameplay. So you have to leave the room (which also leaves the Game Server and returns the client to the Master Server) before you can JoinOrCreateRoom()...
     
  3. ookumspookuminc

    ookumspookuminc

    Joined:
    Jul 30, 2018
    Posts:
    6
    Hello and sorry for the late response,

    I actually am calling LeaveRoom before I try to create another room (see screenshot in original post), but for some reason, the client state gets stuck in the "Leaving" state and never actually calls OnLeftRoom or reconnects to the Master. I'm not too sure what's going wrong.
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,990
    Leaving a room will also switch servers, so it's taking a moment. Wait for the callback which tells you PUN is back on the Master Server, then do the matchmaking. Errors in the logs typically point out the names of the methods to wait for.

    If you are not using the latest PUN 2, update. There was one fix for this a while ago.
     
  5. ookumspookuminc

    ookumspookuminc

    Joined:
    Jul 30, 2018
    Posts:
    6
    Are there other methods to wait for besides OnLeftRoom or OnDisconnected? I have tried LeaveRoom and Disconnect and for some reason they both keep the client state in "Leaving" and the callbacks never execute during gameplay, only when I stop running it in the editor.

    I am using PUN 2 2.15 from the asset store and I didn't see a more up to date version.
     
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,990
    Wait for callback: OnConnectedToMaster.
     
  7. elmtim

    elmtim

    Joined:
    May 27, 2017
    Posts:
    1
    Oh I had the same Problem.
    It turned out, my problem was , that whily try leaving the timescale was 0. By setting time. Time.timeScale = 1 is worked ;D
     
    Valcon and MMHouston like this.
  8. hako-975

    hako-975

    Joined:
    Aug 19, 2020
    Posts:
    3
    Hello dude, try adding this script: Time.timeScale = 1;
    above the script: PhotonNetwork.LeaveRoom();
     
  9. OmarKH72007

    OmarKH72007

    Joined:
    Nov 14, 2021
    Posts:
    1
    if any one wanna return to the lobby in his game
    this code for you

    public void LeaveRoom() // define function and join it with button
    {
    PhotonNetwork.LeaveRoom();
    PhotonNetwork.AutomaticallySyncScene = false;
    PhotonNetwork.LoadLevel(0);
    }
     
  10. sayantansk13

    sayantansk13

    Joined:
    Jul 3, 2022
    Posts:
    1
    Thank You so much asshole,u saved my day!!