pawnStateException: Object is not spawned error on Disconnecting Clients

I want a spawned client to move to a new scene. So to begin this move, a ServerRpc is called, where the player is first despawned and then disconnected from the current scene.

[ServerRpc (RequireOwnership =false)]
    public void DestroyPlayerServerRpc(ServerRpcParams serverRpcParams = default)
    {
        var clientId = serverRpcParams.Receive.SenderClientId;
        if (NetworkManager.ConnectedClients.ContainsKey(clientId))
        {
            if(PlayersInGame.TryGetValue(clientId, out var player))
            {
                player.GetComponent<NetworkObject>().Despawn(true);
                Debug.Log(clientId);
                NetworkManager.Singleton.DisconnectClient(clientId);
            }
            if(clientId == OwnerClientId)
            {
                NetworkManager.Singleton.Shutdown();
                SceneManager.LoadSceneAsync("Room 2");
            }
        }
    }

However when I disconnect, I get an error on the host which says -
SpawnStateException: Object is not spawned
Unity.Netcode.NetworkSpawnManager.DespawnObject (Unity.Netcode.NetworkObject networkObject, System.Boolean destroyObject) (at Library/PackageCache/com.unity.netcode.gameobjects@1.4.0/Runtime/Spawning/NetworkSpawnManager.cs:670)

The client however does move to the next scene.

How can I disconnect clients and move them to new scenes without affecting the first scene where all clients assemble.

You don’t need an rpc for this. Subscribe to the OnClientStopped event and inside that callback method have your LoadScene call. Calling Shutdown on the client will trigger the OnClientStopped event.

For example:

    public void OnClickStartClient()
    {
        NetworkManager.Singleton.OnClientStopped += OnClientStopped;
        NetworkManager.Singleton.StartClient();
    }

    public void OnClickShutdown()
    {
        NetworkManager.Singleton.Shutdown();
    }

    private void OnClientStopped(bool wasHost)
    {
        Debug.Log("OnClientStopped wasHost: " + wasHost);

        SceneManager.LoadSceneAsync("Room 2");
    }