How Re-spawn a player?

I want to do something like when car upsidedown and player use the Re-Spawn key?

this is the code I use :

Header file :
// I use my own Player Controler Class.

UPROPERTY(EditDefaultsOnly, Category = Setup)
			TSubclassOf<APlayerController> SpawnPlyer;

.cpp file :

{
	auto GetActorloc = GetOwner()->GetActorLocation();
	auto GetActorRot = GetOwner()->GetActorRotation();

	Destroy();
	
	if (!ensure(SpawnPlyer)) { return; }
	GetWorld()->SpawnActor<APlayerController>
		(
			SpawnPlyer,
			GetActorloc,
			GetActorRot

			);
}

but this code makes the unreal crash.

please let me know too if this the correct way of doing it, but you don’t know why its crash cause I even don’t know its the correct way or not.

Any help is appreciated
thanks.

I try this one too, but I receive crash again :

{
	auto GetActorloc = GetOwner()->GetActorLocation();
	auto GetActorRot = GetOwner()->GetActorRotation();
	GetController()->UnPossess();
	Destroy();

	if (!ensure(SpawnPawn)) { return; }
	auto charcter =GetWorld()->SpawnActor<APawn>
		(
			SpawnPawn,
			GetActorloc,
			GetActorRot

			);
	GetController()->Possess(charcter);
}

I finally did it with some minor changes you could use it too, just in Cast part Cast it to your pawn class.

{
	auto GetActorloc = GetWorld()->GetFirstPlayerController()->GetPawn()->GetTargetLocation();
	auto GetActorRot = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorRotation();

	GetActorloc = FVector(GetActorloc.Y + 0, GetActorloc.X + 0, GetActorloc.Z + 600);
	GetWorld()->GetFirstPlayerController()->GetPawn()->Controller->UnPossess();

	auto GameMode = GetWorld()->GetAuthGameMode()->DefaultPawnClass;

	if (!ensure(GameMode)) { return; }
	
		GetWorld()->SpawnActor<APawn>
			(
				GameMode,
				GetActorloc,
				GetActorRot

				);
		//If you want detroy the old pawn
		//Destroy();

		TArray<AActor*> FoundActors;
		UGameplayStatics::GetAllActorsOfClass(GetWorld(), ATank::StaticClass(), FoundActors);

		
		auto LastPawn = Cast<ATank>(FoundActors[FoundActors.Num() - 1]);



		if (!ensure(LastPawn)) { return; };
			GetWorld()->GetFirstPlayerController()->Possess(LastPawn);
		
	
}

But there is a minor problem but i dont think its relate to this code you can read more on :