Playing audio on server event

Hey there,
I have server authoratative actions in my shooting game like “successfully hit” sound effect etc
Im starting to toy with audio and playing sounds so I might be going about this the completely wrong way.
I am currently using

AudioSource.PlayClipAtPoint(deathSound, transform.position);

What is the recommended method to get an audio clip to play on all machines at the location?
Do I just write a simple prefab prefab with an audio clip and network object and spawn it or is there a better way to “make an audio clip play at location for all clients”?, heck, is there a built in method on a network behaviour or something?

sorry, thought I’d give more context for where/how this sits.
I have this script sitting on a spell projectile:

public class DestroyOnCollision : NetworkBehaviour
{
    private ProjectileCommonParams projectileParams;
    [SerializeField] private bool withOwner;
    [SerializeField] private AudioClip deathSound;
    void Start()
    {
        projectileParams = GetComponent<ProjectileCommonParams>();
    }

    // Update is called once per frame

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (!IsServer)
            return;
        var physicsObject = collision.gameObject.GetComponentInChildren<PhysicsObject>();
        if (physicsObject != null)
        {
            if (collision.gameObject.TryGetComponent<SpellTarget>(out var spellTarget))
            {
                if (withOwner || !spellTarget.IsInvalidTarget(projectileParams.playerId))
                {
                    DestroyThisAndApplyBuffs(collision.gameObject);
                }
            }
        }
    }

    private void DestroyThisAndApplyBuffs(GameObject collisionGameObject)
    {
        if(TryGetComponent<AppliesBuff>(out var appliesBuff))
        {
            appliesBuff.ApplyBuff(collisionGameObject);
        }
        if (deathSound != null)
        {

            AudioSource.PlayClipAtPoint(deathSound, transform.position);
        }
        Destroy(gameObject);
    }
}

or is this one that works well with clientRPCs? sorry for spam, just spitballing

Hi there,
If you want to destroy NetworkObject you should use “.Despawn()” method because this method triggers on all players.
You mentioned above, you can use “clientRPCs” or listen to “OnNetworkSpawn” method then play the audio.

If something dies, that state change has already been communicated over the network. Whatever that thing is, when it responds to its death it plays a sound fx indiscriminately, meaning it shouldn‘t check if it‘s the owner or a local instance or whatever. It just play the audio and other things (vfx, animation) when it dies.

1 Like

Hi @Tsunamisukoto , all you need to do is to call a ClientRPC from the server. You don’t need to pass the AudioClip to it as you can retrieve it locally (I.E: assign it in the inspector), just the position.

Does this help?

1 Like

Riku that helps immensely and suits how I like to code things. Thanks everyone.

1 Like

Hi Riku, i am having the same problem audiosource is a non nullable value so it does not serialize it and i cant play it on server rpc, how exactly can i play the audio source

Your code should never play sounds on a server. Servers are for handling logic. Always assume a server has no local player, or you’ll have serious issues when implementing server-authoritative models proeprly.

You need to change the logic of your code so that the clip is played on client, I.E: through a ClientRPC