Can someone explain how TargetRPC works?

Im trying to get my server to connect to the client that last hit the mob and then execute a function on that client only

So I have my game setup and my enemies are spawned by the server. They carry the variable that holds the object of the last player that hit it.on the server.

So once the enemy dies on the server it should find that players object and then tell that client only to execute a function which increases its exp

How do I do it using this?
https://docs.unity3d.com/ScriptReference/Networking.TargetRpcAttribute.html

PlayerConfig.cs

Combat.cs

You might be better off with a [SyncVar] exp variable on each player. Then increase the value on the server, and it will trickle down to the client(s).

But if you want to use TargetRPC then on the player that killed the monster call your TargetRPC method on the server. Pass the networkConnection of the owner of that player. Then the TargetRPC will only be called on the client owner of that player.

How do I use the targetRPC method?

https://docs.unity3d.com/ScriptReference/Networking.TargetRpcAttribute.html

using UnityEngine;
using UnityEngine.Networking;

public class Example : NetworkBehaviour {

    [TargetRpc]
    public void TargetDoMagic(NetworkConnection target, int extra)
    {
        Debug.Log ("Magic = " + (123 + extra));
    }


    [Command]
    void CmdTest()      
        TargetDoMagic(connectionToClient, 55);
    }
  
}

Here is what I tried but I am getting an error

I have this on the enemy that is controlled by the server

  [TargetRpc]
    public void TargetDoMagic(NetworkConnection target)
    {


                int amountToGive = Random.Range(minExp, maxExp);
                //ADJUST EXP ON CHARACTER THAT LAST HIT
                
                //   NetworkServer.SendToClient(playerID, sInt, );
                playerLastHit.recieveExp(amountToGive);
           
        }


    [Command]
    void CmdGIVEEXPv2()
                {
                    if (PlayerLastObject != null && isServer)
                    {
                        playerLastHit = PlayerLastObject.GetComponent<PlayerConfig>();
                        if (playerLastHit != null) {
                //   int playerID = playerLastHit.GetComponent<NetworkConnection>().connectionId;
                          
 [I][B]               TargetDoMagic(playerLastHit.GetComponent<NetworkConnection>());[/B][/I]
                        }
                    }
    }

bold line is where I get the error

ArgumentException: GetComponent requires that the requested component 'NetworkConnection' derives from MonoBehaviour or Component or is an interface.
UnityEngine.Component.GetComponent[NetworkConnection] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/ComponentBindings.gen.cs:45)
Combat.CmdGIVEEXPv2 () (at Assets/_SCRIPTS/Combat.cs:48)
Combat.CallCmdGIVEEXPv2 ()
Combat.TakeDamage (Int32 amount) (at Assets/_SCRIPTS/Combat.cs:87)
Bullet.OnCollisionEnter (UnityEngine.Collision collision) (at Assets/_SCRIPTS/Bullet.cs:12)

I know this post is a bit old but I can’t help to reply here.

The reason why this here isn’t working is because you tried to use GetComponent on a non-monobehavior script and is in fact, IDisposable.

https://bitbucket.org/Unity-Technologies/networking/src/78ca8544bbf4e87c310ce2a9a3fc33cdad2f9bb1/Runtime/NetworkConnection.cs?at=5.3&fileviewer=file-view-default