The player who leaves second is not being able to create or join room unity

Options
Hey Everyone,
I am facing this issue and it's stopping my progress. I am making a chess game with multiplayer. I was successful in creating a room and make other player join, but once the game is over and one of the players leave the game then the player who leaves second is not being able to create a room or join a new room for a new game and gets an error "JoinRandomRoom failed. Client is on GameServer (must be Master Server for matchmaking) but not ready for operations (State: Leaving). Wait for callback: OnJoinedLobby or OnConnectedToMaster."
Doesn't matter if the player was a master client or not, if they are leaving second? they won't be able to join or create a new room.

once the game is over and I am switching back to menu, I am checking couple of things in mainmenu like,
1. Am i still in a room? result is always false
2. Am i even connected to server? result is always true

Here is the code that works in main menu
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;


public class NetworkController : MonoBehaviourPunCallbacks
{
    private void Start()
    { 

        if(PhotonNetwork.InRoom)
        {
            Debug.Log("why am i In a room in start?");  
            PhotonNetwork.LeaveRoom();
        }
        if (PhotonNetwork.InLobby)
        {
            Debug.Log("why am i In a Lobby in start?");
            PhotonNetwork.LeaveLobby();
        }
        if (PhotonNetwork.IsConnected)
        {
            Debug.Log("We are connected to the " + PhotonNetwork.CloudRegion + " server! (Btw calling from start)");
        }
        else
        {
            Debug.Log("We are not connected to the " + PhotonNetwork.CloudRegion + " server! (Btw still calling from start)");
        }
  
        Connect();

    }


    private void Update()
    {
        if (!PhotonNetwork.IsConnected) 
        {
            Connect();
        }
    }

    public override void OnConnectedToMaster()
    {
        Debug.Log("We are connected to the " + PhotonNetwork.CloudRegion + " server!");
    }

    public override void OnLeftLobby()
    {
        Connect();
    }

    private void Connect()
    {
      //  PhotonNetwork.JoinLobby();
        PhotonNetwork.ConnectUsingSettings();
 
    }
}


Here is the code that works after user presses button for main menu
 public void OnHome()
    {
        if (PhotonNetwork.InRoom)
        {
            OnClickDisconnect();
            goHome = true;
        }
        else
        {
            SceneManager.LoadScene("Main Menu");
        }
    }

    public void OnQuit()
    {
        OnClickDisconnect();
        Application.Quit();
    }

 public void OnClickDisconnect()
    {
        PhotonNetwork.LeaveRoom(); /// With only this line, local player will leaveRoom and join MasterServer, which I don't want. (In this example, he will reconnect to the room)
        PhotonNetwork.LeaveLobby();   
    }

    public override void OnLeftRoom()
    {
        if(goHome == true)
        {
            goHome = false;
            SceneManager.LoadScene("Main Menu");
        }
    }

Can you please help me solve this problem? I am going crazy!
No matter what i tried, the second player just won't be able to join or create a new room.

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Hi @bhavin2707,

    Thank you for choosing Photon!

    See my comment on stackoverlow here.
  • bhavin2707
    edited April 2020
    Options
    @JohnTube Thank you so much for replying. I just saw your comment and replied back. I hope you check it out. posting the comment here too since it seems photon related internal issue.

    Hii @JohnTube .I updated the post and added code for creating and joining the room(Posted Below). The thing is I am not instantly trying to create a room as soon as I get back to the menu. The player creates room by pressing the respective buttons (and I think have enough time to connect to master in between). But, I don't think that's the problem. I noticed one thing that when the second player tries to leave room or disconnect, It gets stuck on state "Leaving" or "Disconnecting" respectively. OnRoomLeft and OnDisconnect are never called for second players no matter how long I wait and stay on the game server.

    Edit 1: Code for joining room as requested by @JohnTube

    DelayStart Function is responsible for joining the lobby or creating the room in case joining fails. It is only called when player presses "Play online > Places a bet > Start Game(this is where it's called)"
    public override void OnConnectedToMaster()
        {
            PhotonNetwork.AutomaticallySyncScene = true;
            delayStartButton.SetActive(true);
        }
    
        public void DelayStart()
        {
            int bet = MenuHandler.instance.betAmount;
            if (bet < 500)
                bet = 500;
    
            int currentMoney = MenuHandler.instance.currentMoney;
            if (currentMoney < bet)
            {
                MenuHandler.instance.popup_window.SetActive(true);
                return;
            }
            MenuHandler.instance.AddMoney(-bet);
            Hashtable expectedCustomRoomProperties = new Hashtable() { { "Bet", bet } };
            PhotonNetwork.JoinRandomRoom(expectedCustomRoomProperties, (byte)roomSize);
    
          //  PhotonNetwork.JoinRandomRoom();
        }
    
        public override void OnJoinRandomFailed(short returnCode, string message)
        {
            Debug.Log("Creating random room failed!!");
            CreateRoom();
        }
    
        private void CreateRoom()
        {
            Debug.Log("Creating Room Now");
            int randomRoomNumber = Random.Range(0, 10000);
    
            int bet = MenuHandler.instance.betAmount;
            if (bet < 500)
                bet = 500;
    
            RoomOptions roomOps = new RoomOptions() { IsVisible = true, IsOpen = true, MaxPlayers = (byte)roomSize, CustomRoomProperties = new ExitGames.Client.Photon.Hashtable() { { "Bet", bet } } };
            roomOps.CustomRoomPropertiesForLobby  = new string[] { "Bet", bet.ToString()};
    
    
            PhotonNetwork.CreateRoom("Room" + randomRoomNumber, roomOps);
    
        }
    
        public override void OnCreateRoomFailed(short returnCode, string message)
        {
            Debug.Log(message);
           // CreateRoom();
        }
    
  • AdamSingle
    Options
    This almost sounds like my issue, although since I'm testing with a build and an in editor player, I hadn't put it down to just being the second player to leave.
    https://forum.photonengine.com/discussion/16301/cant-create-room-after-leaving-one#latest