Server/Client problem : rpc call

Hello everyone !

First of all, merry christmas :slight_smile:

I have a problem in my FPS game when I try to handle damages with a specific weapon.

This script is attached to a player’s knife :

var isShootenBy:NetworkPlayer;
private var firstShootDone:boolean = false;

function OnTriggerEnter (other:Collider)
{
	if (firstShootDone) return;
	if (other.networkView && other.networkView.owner == isShootenBy) return;
	
	if (other.gameObject.tag == "joueur")
	{
		firstShootDone = true;
		Debug.Log("joueur touché au couteau");
		Hit(other.transform);
	}
	yield WaitForSeconds(0.24f);
	firstShootDone = false;
}

function Hit(obj:Transform) {
	var currParent:Transform = null;
	if (!obj.parent) currParent = obj;
	else
	{
		var nextParent:Transform = currParent;
		while (nextParent)
		{
			currParent = nextParent;
			nextParent = nextParent.parent;
		}
	}
	
	currParent.GetComponentInChildren(PlayerScript).networkView.RPC("IsHitBy", currParent.networkView.owner, 0, isShootenBy);
}

It sends to the shot player some intel like what weapon hit him, and who shot him.

Then, on my player game object I have a PlayerScript attached to its son that contains the “isHitBy” method :

@RPC
function IsHitBy (from:int, by:NetworkPlayer) {
	if (isDead) return;
	var damages = 0;
	switch (from) {
		case Weapon.Couteau:
			damages = couteauDamage;
			break;
		case Weapon.Arbalete:
			damages = arbaleteDamage;
			break;
		case Weapon.LanceFlammes:
			damages = lanceFlammesDamage;
			break;
		case Weapon.Minigun:
			damages = minigunDamage;
			break;
	}
}

My problem is the following : when the client player hits the server player, the server player looses life (correct behavior), when the server player hits the client player, I have a core dump, big crash and the windo closes…

So here is what I already tried to find the problem : if I comment the rpc call on IsHitBy from my knife script, everything is fine (but damages are not applied anymore … logic), if I let this line uncomment, but I comment everything IN IsHitBy, then I still have a core dump. Both my players are instantiated in the same way, there are both instances of the same prefab.

Thanks by advance to anyone who can help me !

One problem I can identify immediately is that the player cannot send RPC to another player directly by themselves, unless that other player is the server.

Because the server is connected to everyone, the server knows the other players by ‘name’ (i.e. by their NetworkPlayer struct) so the server can refer to each player individually.

The clients, on the other hand, are only directly connected to the server, so they can only ever send an RPC to the server. If they send an RPC with a RPCMode different than 'RPCMode.Server` what happens is that the player sends the RPC to the server, which then relays the RPC to the relevant players, according to the flag (All or Others).

This is again, because the player can’t send an RPC to others by themselves.