# How do I disconnect from a Dedicated Server the right way?

**URL:** <https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905>\
**Category:** Multiplayer & Networking\
**Tags:** Networking, dedicated-server, unreal-engine, question\
**Created:** [October 29, 2015, 7:20pm UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905 "2015-10-29T19:20:03Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spiris](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Spiris](https://forums.unrealengine.com/u/Spiris)\
**Post date:** [October 29, 2015, 7:20pm UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/1 "2015-10-29T19:20:03Z")

</div>

Simple question I feel, but right now the only way I can do this successfully is to attempt client travel that cannot occur, which forcibly disconnects the client from the server, I then destroy the session. I have tried the Logout node and the Destroy Session node by themselves and in unison, however it fails to send me back to the login screen, and the player controller can no longer interact with any online blueprint nodes. I would expect there to be a single command that disconnected the client from the server and then left the rest of the decisions up to the game developer.  
Hope someone has figured this out.  
Spiris

---

<div class="post-metadata">

**Author:** ![Rhynedahll](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Rhynedahll](https://forums.unrealengine.com/u/Rhynedahll)\
**Post date:** [January 28, 2016, 9:43pm UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/2 "2016-01-28T21:43:48Z")

</div>

I’m looking for a way to do this as well.

---

<div class="post-metadata">

**Author:** ![Spiris](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Spiris](https://forums.unrealengine.com/u/Spiris)\
**Post date:** [March 21, 2016, 3:51pm UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/3 "2016-03-21T15:51:50Z")

</div>

I hate to say it, but this is still a problem. In this situation is there a call required on the server first to inform the client they are being disconnected? The destroy session node sounds like a terrible idea on the server, but perhaps the ‘logoff’ node will work. This is currently not clear, however, and documentation references or someone with the knowledge would be appreciated.

---

<div class="post-metadata">

**Author:** ![Spiris](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Spiris](https://forums.unrealengine.com/u/Spiris)\
**Post date:** [May 6, 2016, 3:59pm UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/4 "2016-05-06T15:59:40Z")

</div>

This thread has been a long time running, apparently without any advice. Not to mention that I am seeing updates about comments that aren’t even visible on it. Is this something Unreal Engine simply cannot do? It seems like a huge viability concern for anyone making an online game…

---

<div class="post-metadata">

**Author:** ![mdonge](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mdonge](https://forums.unrealengine.com/u/mdonge)\
**Post date:** [May 19, 2016, 7:21am UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/5 "2016-05-19T07:21:27Z")

</div>

If you use sessions nodes, there is a “logout” node to disconnects a player, you just need his controller !!!

---

<div class="post-metadata">

**Author:** ![Spiris](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Spiris](https://forums.unrealengine.com/u/Spiris)\
**Post date:** [June 10, 2016, 6:06pm UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/6 "2016-06-10T18:06:36Z")

</div>

Hello again everyone,  
I did a large amount of digging, but I stumbled upon a workaround. First to answer my question:

## You cannot disconnect from a dedicated server correctly. But that is ok.
It is currently not supported by the engine. Whatever the case, mistakes were made and ignored. Moving on. Now for the workaround. With tips and tricks for things that aren't terribly transparent.
### My Workaround
To Disconnect Intentionaly: Destroy the Session, then on success or fail, open your login level. This will handle disconnecting you to an extent. To Disconnect on accident: Check to be sure you are receiving a network or travel error on the client, then open your login level. 
### Now for the magic
You may be in a situation where you cannot log back in. This is where the error was the hardest to identify, but basically the session you would have thought got destroyed earlier in fact still lives. This may be a network error issue, I dont know. So long as that session lives you cannot reconnect to any server. So in your connection logic, simply call DestroySession first. On Success or Fail attempt to join session and you should be clear. 
## Tricks
1) UE4 has a default engine map called "Entry". If you name your login level "Entry" THAT IS BAD. The engine will get confused at some point and try to reroute you to that level, which is not your login level, and the player has to deal with that. Name your map something else and you will be saved from my headache. . 2) You may need to destroy your player controller on intentional logging out. You may not. This is still a gray zone for me as sometimes it is necessary, and sometimes opening the new level handles correctly. If there is anything else anyone can clarify for this, any info is good info. :) ~Spiris

---

<div class="post-metadata">

**Author:** ![jinzay](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@jinzay](https://forums.unrealengine.com/u/jinzay)\
**Post date:** [February 22, 2018, 3:36pm UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/7 "2018-02-22T15:36:56Z")

</div>

Just found this. The tip to “destroy session” everytime before creating a new one seems so obvious. Helped me alot! Thanks

---

<div class="post-metadata">

**Author:** ![Jay2645](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jay2645](https://forums.unrealengine.com/u/Jay2645)\
**Post date:** [June 15, 2019, 3:22am UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/8 "2019-06-15T03:22:45Z")

</div>

This is a bit old, but I was running into this issue fairly recently. I wanted the server to close all the client’s connections once the match was over.

Destroying the PlayerControllers like the accepted answer says to do _worked_, but it caused all the clients to “panic” because from their perspective they were losing connection. This had the potential to leave either the client or server in a weird state.

The solution was to call `GetEngine()->HandleDisconnect(World, World->GetNetDriver());` from the _client_. I replicated the current match “phase” to the clients on the GameState, and when that phase was modified it checked if it was the “match end” phase. If so, the client called the above function and disconnected itself.

You can look inside the code for UGameInstance for examples; there’s also a `ReturnToMainMenu` function in the GameState which I didn’t try but seems fairly promising.

---

<div class="post-metadata">

**Author:** ![goldenspiral](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/goldenspiral/32/551832_2.png) [@goldenspiral](https://forums.unrealengine.com/u/goldenspiral)\
**Post date:** [December 29, 2020, 10:39am UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/9 "2020-12-29T10:39:52Z")

</div>

There is a some delay to the moment of detecting disconnect event. Maybe i have delay because logs .

---

<div class="post-metadata">

**Author:** ![Makhran](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Makhran](https://forums.unrealengine.com/u/Makhran)\
**Post date:** [May 8, 2021, 4:38pm UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/10 "2021-05-08T16:38:09Z")

</div>

I will share the way that I found. In Blueprints, use “Execute Console Command” (Command = “disconnect”, Specific Player = Player Controller which needs to be disconnected from the server). After executing this command, the player automatically opens the level that is specified by default in the project settings.  
BTW this raises the “NetworkError” event (Connection Lost and Failure Received) In the player’s GameInstance.

---

<div class="post-metadata">

**Author:** ![ERYNEK3D\_GAMES](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ERYNEK3D\_GAMES](https://forums.unrealengine.com/u/ERYNEK3D_GAMES)\
**Post date:** [June 29, 2021, 10:20am UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/11 "2021-06-29T10:20:21Z")

</div>

Worked for me, thx bro.

---

<div class="post-metadata">

**Author:** ![ChinyONE](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ChinyONE](https://forums.unrealengine.com/u/ChinyONE)\
**Post date:** [August 26, 2021, 9:49pm UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/12 "2021-08-26T21:49:20Z")

</div>

## Had the same problems:

## My Steps to join / leave ded. server:

1. Dedicated Server and Client Setup
2. Client connecst first time to Server:
3. Then Leaves: by first killing existing Session, then loading different Map in the OnLogout Method in my GameMode  
4.Connect for the second time:

## Result:

- Player could connect first time  
normally, but second time he wasn´t able to be  
spawned correctly.

## The Problem:

- I´ve overriden the Event:  
HandleStartingNewPlayer and my  
Respawning code was flawed.

* * *

Taking a look at the Engine Code made my mistake clear. A lot is being done in the GameMode and GameModeBase Classes for you. I tried to impl. very poor spawning logic, which broke the spawning system.

This is the code fromGameModeBase.cpp L: 1017

```
void AGameModeBase::HandleStartingNewPlayer_Implementation(APlayerController* NewPlayer)
{
	// If players should start as spectators, leave them in the spectator state
	if (!bStartPlayersAsSpectators && !MustSpectate(NewPlayer) && PlayerCanRestart(NewPlayer))
	{
		// Otherwise spawn their pawn immediately
		RestartPlayer(NewPlayer);
	}
}

```

---

<div class="post-metadata">

**Author:** ![ChinyONE](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ChinyONE](https://forums.unrealengine.com/u/ChinyONE)\
**Post date:** [August 26, 2021, 9:49pm UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/13 "2021-08-26T21:49:39Z")

</div>

Depending on what GameMode u use: GameMode or GameModeBase, has it´s own way of spawning players and changeing the game state.  
If you override one or more of those methods in your game mode, you need to call the other depending methods accordingly to make the default GameMode Code work.

I did not do that, and my spawning code was flawed. So the spawning didn´t work anymore, after spawning the first client in the map.

## NOTE:

- Check the Engine Code for reference and don´t just overriding methods, like me…Make sure to read the code and get a grip of what the function is supposed to do at a minimum.
- When customizing the spawning logic you can pretty much just override the methods used in GameMode or GameModeBase from cpp or Blueprint.
- for disconnecting a player from a dedicated server those two methods work always:

## Solutions that will work for dedicated servers:

A. Join / Disconnect Dedicated Server  
by IP: (No Sessions)

- JOIN: `open <IPADRESS:PORT>` | Node:  
Execute Command or OpenLevel works
- DISCONNECT: `disconnect` | Node: Execute Command

* * *

B. Join / Disconnect Dedicated Server by Sessions  
(Advanced Sessions and regular Online Subsystem from epic will work)

- JOIN: use Node: Join Session | Needs an Session Result BP Object reference and PlayerController Ref
- DISCONNECT: use Node: Destroy Session | Needs PlayerController Ref

---

<div class="post-metadata">

**Author:** ![RealGAMEBR0VIP1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@RealGAMEBR0VIP1](https://forums.unrealengine.com/u/RealGAMEBR0VIP1)\
**Post date:** [April 18, 2023, 5:17am UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/14 "2023-04-18T05:17:55Z")

</div>

Seems to work fine for me. can rejoin the server after I leave as well and create new sessions. Only commenting to add to the collection of fixes here for anyone looking in the future.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/5/7/4573518431eb90fd133b2741650bf0dc870f014d.png)

---

<div class="post-metadata">

**Author:** ![pawciut](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pawciut/32/476660_2.png) [@pawciut](https://forums.unrealengine.com/u/pawciut)\
**Post date:** [February 26, 2024, 7:47pm UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/15 "2024-02-26T19:47:59Z")

</div>

Thank you @Jay2645 ! I’ve been looking for this for few hours.

---

<div class="post-metadata">

**Author:** ![Zhinkk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zhinkk](https://forums.unrealengine.com/u/Zhinkk)\
**Post date:** [December 25, 2024, 9:31pm UTC](https://forums.unrealengine.com/t/how-do-i-disconnect-from-a-dedicated-server-the-right-way/336905/16 "2024-12-25T21:31:57Z")

</div>

In all of the solutions I’ve tried in this thread the player actor remains present and visible on the dedicated server. Does nobody else have this issue?

I’m really surprised to see such a basic functionality not implemented as a first class API.
