NetworkManager.DisconnectClient() is causing the Host to disconnect as well

I’m trying to implement a function to Kick a player out of the Server, but upon adding this function to a button event:

NetworkManager.Singleton.DisconnectClient(1, "Kicked by LobbyHost.");

It kicks BOTH the Client with ID 1 and the Host with ID 0.

Logs For Client Disconnect:

Disconnected Client ID: 1
UnityEngine.Debug:Log (object)
AbstractLobbyManager/<>c:<Start>b__14_0 (ulong) (at Assets/Scripts/UI/AbstractLobbyManager.cs:72)
Unity.Netcode.NetworkConnectionManager:InvokeOnClientDisconnectCallback (ulong) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs:140)
Unity.Netcode.NetworkConnectionManager:OnClientDisconnectFromServer (ulong) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs:1028)
Unity.Netcode.NetworkConnectionManager:DisconnectRemoteClient (ulong) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs:1055)
Unity.Netcode.NetworkConnectionManager:DisconnectClient (ulong,string) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs:1085)
Unity.Netcode.NetworkManager:DisconnectClient (ulong,string) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs:1140)
AbstractStandardLobbyManager/<>c:<ListPlayers>b__6_1 () (at Assets/Scripts/UI/AbstractStandardLobbyManager.cs:236)
UnityEngine.EventSystems.EventSystem:Update () (at ./Library/PackageCache/com.unity.ugui/Runtime/UGUI/EventSystem/EventSystem.cs:530)

Logs For Host Disconnect which happens after the Client Disconnects above:

Disconnected Client ID: 0
UnityEngine.Debug:Log (object)
AbstractLobbyManager/<>c:<Start>b__14_0 (ulong) (at Assets/Scripts/UI/AbstractLobbyManager.cs:72)
Unity.Netcode.NetworkConnectionManager:InvokeOnClientDisconnectCallback (ulong) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs:140)
Unity.Netcode.NetworkConnectionManager:DisconnectEventHandler (ulong) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs:502)
Unity.Netcode.NetworkConnectionManager:HandleNetworkEvent (Unity.Netcode.NetworkEvent,ulong,System.ArraySegment`1<byte>,single) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs:396)
Unity.Netcode.NetworkConnectionManager:PollAndHandleNetworkEvents () (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs:370)
Unity.Netcode.NetworkManager:NetworkUpdate (Unity.Netcode.NetworkUpdateStage) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs:44)
Unity.Netcode.NetworkUpdateLoop:RunNetworkUpdateStage (Unity.Netcode.NetworkUpdateStage) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkUpdateLoop.cs:192)
Unity.Netcode.NetworkUpdateLoop/NetworkEarlyUpdate/<>c:<CreateLoopSystem>b__0_0 () (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkUpdateLoop.cs:215)

All my code is calling is that Disconnect function, any idea why it is also Disconnecting the Server/Host?

Maybe because the host is the client with ID 1? :wink:
Or the button is not hooked up correctly and fires twice.

In any case: don’t hardcode that number. The numbers will change when a client connects and disconnects and reconnects. Get it from the NetworkManager clients list for instance.

I’m Disconnecting ClientID 1 deliberately because it is the Client, not the host, was initially getting ClientID from ConnectedClients, but it didn’t work hence I’m hardcoding it to try see if I got the wrong ID.

Also if the Host attempts to DisconnectClient itself, there will be a warning preventing it from doing so, so that cannot be the case based of what I’ve tested and read. Screenshot:
image

I’ve tested and can confirm Client 1 is the Client and have Debug Logs as well. I just have no clue WHY it is disconnecting the Host right after the Client is disconnected via NetworkManager.DisconnectClient(1);

Try adding my NetworkEventLogger on the NetworkManager object. You’ll see exactly what’s going on, the start/stop and connect/disconnect events but also transport failure.

Particularly transport failures: often this is totally left unconsidered but a transport failure can occur at any time for various reasons and this will end your network session.

Confirm that you actually receive the ServerStop event after kicking that player. It might be a simple misunderstanding and the host session isn’t actually ending.

1 Like

Thank you, I think the NetworkEventLogger might be able to help me look deeper into this issue.

Have you found a solution to this? I have the same issue, it also happens when a client crashes.

I’m using Facepunch Transport.

When I kick a client, two ConnectionEvent with type ClientDisconnected are invoked, the first one with Client Id 1 and the second one with Client Id 0, which results in the host’s player object being destroyed (I do not handle that myself, the Network Manager creates and destroys my Player objects)

Funny thing is, if a client crashes while they try to connect, only the event with Client Id 0 is invoked, not the other one.

Edit:
I have found the culprit for me, I found the solution here: NGO + Facepunch - Problem when approval is refused for a client