ServerRPC function is called multiple times on client

Hello. I am currently learning about netcode and i am creating a simple fps multiplayer game with netcode.
The problem that i am facing right now is that, i use a ServerRpc call for the shoot function.
When i shoot from the host, it works perfectly, however from the clients, the shoot function is called multiple times. For example, for each mouse click, the function is called 4-5 times.

    void Update()
    {
        if (!IsOwner) return;
        UserInput();
    }

    private void UserInput()
    {

        //Input
        if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
        else shooting = Input.GetKeyDown(KeyCode.Mouse0);

        //Shoot
        if (readyToShoot && shooting && !bulletsLeft > 0){
            ShootServerRpc();
        }
    }



    [ServerRpc]
    private void ShootServerRpc()
    {

        Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
        Vector3 targetPoint;
        targetPoint = ray.GetPoint(75);
   
        //Calculate direction
        Vector3 directionWithoutSpread = targetPoint - attackPoint.position;
        //Spread
        float x = Random.Range(-spread, spread);
        float y = Random.Range(-spread, spread);
        float z = Random.Range(-spread, spread);

        //Calc Direction with Spread
        Vector3 directionWithSpread = directionWithoutSpread + new Vector3(x, y, z);

        //Instantiate bullet/projectile
        GameObject currentBullet = Instantiate(bullet, attackPoint.position, Quaternion.identity);

        currentBullet.transform.forward = directionWithSpread.normalized;

        //AddForce
        currentBullet.GetComponent<Rigidbody>().AddForce(directionWithSpread.normalized * shootForce, ForceMode.Impulse);

        bulletsLeft--;
        bulletsShot--;

        if (allowInvoke)
        {
            Invoke("ShotReset", timeBetweenShooting);
            allowInvoke = false;
        }

        currentBullet.GetComponent<NetworkObject>().Spawn();

    }

Where do you change “readyToShoot” and “bulletsLeft” variable on client side?
Because “ShootServerRpc” is just run on Host side not client side.

Thanks for your reply. I am not changing these 2 variables somewhere else tbh.
So, if im right, you mean that these 2 variables are always the same for the client, because they only change inside the ServerRpc function? So i have to modify them somewhere else, which will only be for the client?

You have to tell the client they changed, for sure. The server should know about these to, because ultimately the server is the one who should decide whether the client can shoot or not (to avoid cheating).

You can have a NetworkVariable to represent this data, or you could move the checks entirely to the server

1 Like

Yes this is exactly what I said.
So, Client runs “ShootServerRpc()” method but doesn’t change any variable.
Because of this “if (readyToShoot && shooting && !bulletsLeft > 0)” condition will always be same and never change.

I can’t see your all code but, I guess you miss that point.

Thank you everyone for your answers, i figured it out.
The concept around Server & Client Rpc’s was a bit confusing to me, but i now start to understand it better.

1 Like