through the main menu to decide which mode to run issue.

I want to through the game main menu to let the player select running as dedicated server or client, I found when initialize the gameinstance, the run mode is set ,

bool UGameInstance::InitializePIE(bool bAnyBlueprintErrors, int32 PIEInstance, bool bRunAsDedicated)

if the first main menu level is running, can I change it from client mode to dedicated mode?

Ehm, I don’t think that’s what you want to do.

For a Dedicated Server, you must compile a separated version of the game.
Also a Player can’t be a Dedicated Server. The idea of that kind of Server is to NOT have a Player.

If you want a Player to be the Server, you need a Listen Host.

Dedicated Server Guide: A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums

My Compendium to learn more about Networking: