Damage function in server only runs when playing as server

Hi!
I am trying to implement a shooting function in C++ for a multiplayer game but me being a total noob in C++, I a currently runnning into an issue where I have successfully managed to create a function that runs in the server but the content of it only runs if I play as the server.

.h file:

	UFUNCTION(Server, Reliable, WithValidation)
		void ApplyDamageServer();
	void ApplyDamageServer_Implementation();
	bool ApplyDamageServer_Validate();

.cpp file:

// Fraction of the shooting function
if (WpnFireHit.GetActor()->ActorHasTag(FName(TEXT("Damageable"))))
{
	TestResult = WpnFireHit;
	TestController = Controller;
	TestDamageTypeClass = DamageTypeClass;
	TestActorCause = this;

	ApplyDamageServer();
}
else
{
	GLog->Log(TEXT("No damage"));
}


//Apply Damage Server Implementation
void AWeaponBase::ApplyDamageServer_Implementation()
{
	GLog->Log("Damage Implement");
	if (GetLocalRole() == ROLE_Authority)
	{
		UGameplayStatics::ApplyPointDamage(TestResult.GetActor(), Damage, TestResult.ImpactPoint, TestResult, TestController, TestActorCause, TestDamageTypeClass); // Apply Damage

		GLog->Log("Authority");
	}
	else
	{
		GLog->Log("No Authority");
	}
}

//Apply Damage Server Validate
bool AWeaponBase::ApplyDamageServer_Validate()
{
	return true;
}

My problem is that when playing as the server, everything runs as expected but when on the clients everything runs but the damage does not run or does not apply but the rest of the funtion does.

For exemple, on the clients “GLog->Log(“Authority”);” runs but the damage does not apply.

Try running the code from within the clients controller. Only classes that can exist between client and server and are owned by the player can call server functions.

You want to make sure your WeaponBase actor is replicated, spawned by the server, and owned by the pawn actor it’s attached to, so you can fire server RPCs freely.

Thanks for your replies!

I will try again as soon as I have some free time.

Watch this video:

then read this page:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.