[RELEASE] [C#] PlayCustomSounds - Players, Coordinates and Reduce Volume based on distance!

London Studios

London Studios - Update

Since forming London Studios in April 2020 we’ve created a number of high quality and premium resources for the FiveM project, focusing on the emergency services and aiming to bring your server to the next level.

Although we made a number of free resources such as this one in the first year, we’ve now switched to creating paid content, keeping them constantly updated and working along with providing the best possible support to our customers.

Our most popular resources now include Smart Fires, Police Grappler and Smart Hose.

With thousands of happy customers we are confident you’ll love our resources and our active support team are on hand to help if you have any questions!

Our store: https://store.londonstudios.net/forum

Therefore, this resource is now likely out of date and is no longer supported by us. The full source code is available should you wish to make any changes. All of our paid resources however are constantly updated and we invite you to take a look!

Play Custom Sounds - London Studios

PlayCustomSounds is a standalone C# FiveM Resource allowing you to play external sounds in your FiveM server from other resources and scripts. Main Functions:

1. Play sounds to all players

2. Play sounds to a specified player

3. Play sounds to a specified player and all players in a certain radius, including a reduction in volume the further away you are.

4. Play sounds to a set 3D coordinate, and all players in a radius, including a volume reduction the further away you are.

This resource works by the triggering of ServerEvents from external scripts, the possibilities are endless, you may choose to play a sound upon a player entering a vehicle, or even play sounds in a certain area of the map such as a custom fire alarm or sound. This is similar to a Lua plugin for external sounds, however this version has more functionality.

Please use the documentation below on how to install and use this in your resources. You may call these external ServerEvents from any FiveM supported language. Any bugs or feedback is greatly appreciated in the thread and we will support any queries with installations or those requiring assistance.

Download Link

Download at GitHub here.
This resource can be downloaded and re-uploaded with other resources you create!
Pull requests are welcomed! We are always looking to improve the source code.

1. Installation

Any external resources triggering ServerEvents handled by this resource (playCustomSounds) will require the server to have this installed. The installation comes with a FiveM-Ready “fxmanifest.lua” file. This should be placed in the server resources file, and ensure you start “playcustomsounds” in server.cfg.

IMPORTANT: The resource name must remain lowercase, this is due to an interaction with the FiveM NUI function requiring this for DNS purposes.

2. Installing custom sounds

All custom sounds must be in the “.ogg” format. Online converters can be used to format files this way. Place all custom sounds in the “html/sounds/” folder in the “playcustomsounds” resource folder.

3. Triggering Custom Sounds

After the PlayCustomSounds resource is on the server, you will have the ability to TriggerServerEvents from any other resource, including ones you create.

These are all Server Events, and triggered from a Client resource file.

(examples, client.lua or Client.net.dll)

NOTES:

  • The specified volume must be at a maximum of 1.0 and must be as a float value.

  • The specified sound file to play must not include .ogg, this is automatically added!

  • The dynamic volume (further away you are), is automatic but requires a base volume.

  • The distance (or radius) is a float value, it is worth testing this out in-game

Play sound to all players - “Server:SoundToAll”

Play a sound to all players on the server

Format:

TriggerServerEvent(“Server:SoundToAll”, soundFile, soundVolume);

soundFile = String, soundVolume = Float

Example:

TriggerServerEvent(“Server:SoundToAll”, “example”, 1.0f);

This would play the “example.ogg” sound at Volume 1.0 to all players.

(Note: The f after 1.0 is used in C# programming to declare a float.)

Play sound to client - “Server:SoundToClient”

Play a sound to a specified client based on NetworkID. (Not other players nearby)

Format:

TriggerServerEvent(“Server:SoundToClient”, networkId, soundFile, soundVolume);

networkId = Specified Player, soundFile = String, soundVolume = Float

Example:

TriggerServerEvent(“Server:SoundToClient”, networkId, “example”, 1.0f);

This would play the “example.ogg” sound at Volume 1.0 to the player with “networkId”.

(Note: The f after 1.0 is used in C# programming to declare a float.)

Play sound to player and radius - “Server:SoundToRadius”

Play a sound to the specified player based on NetworkID and all players in a specified radius to the player, reducing the volume based on distance from the player.

Format:

TriggerServerEvent(“Server:SoundToRadius”, networkId, soundRadius, soundFile, soundVolume”);

networkId = Specified Player, soundRadius = Float, soundFile = String, soundVolume = Float

Example:

TriggerServerEvent(“Server:SoundToRadius”, networkId, 20.0f, “example”, 1.0f);

(Note: The f after 1.0 is used in C# programming to declare a float.)

This would play the “example.ogg” sound at Volume 1.0 to the player with “networkId” and all players in a 20.0 float radius, reducing the volume based on distance.

Play sound to coords and radius - “Server:SoundToCoords”

Play a sound to the specified coordinates (3D) and all players in a specified radius, reducing the volume based on distance from the coordinates.

Format:

TriggerServerEvent(“Server:SoundToCoords”, positionX, positionY, positionZ, soundRadius, soundFile, soundVolume);

positionX, positionY, positionZ = Floats, soundRadius = Float, soundFile = String, soundVolume = Float.

Example:

TriggerServerEvent(“Server:SoundToRadius”, positionX, positionY, positionZ, 20.0f, “example”, 1.0f);

(Note: The f after 1.0 is used in C# programming to declare a float.)

This would play the “example.ogg” sound at the specified coordinates at volume 1.0 and all players in a 20.0 float radius, reducing the volume based on distance.

One way these coordinates can be declared prior is through the use of Vector3, a FiveM Native Function, you can then enter positionX, positionY and positionZ as parameters.

Licence Information

© 2020 - London Studios
GNU General Public License v3.0

13 Likes

Nice Script ! Looking forward to Use it in my projects.
Can we have Access to Source ?

@rezaCyber

Hi,
Thanks for your comments. We are glad you will be using it in your projects.

If you have any ideas for new functions for a future update on this resource, please let us know and we will be more than happy to implement this for you.

Best wishes,
London Studios

1 Like

Releases require source code

Hello. I personally don’t recommend creating a Server:SoundToAll. Without reading the code, I can already tell this would be vulnerable (and fun) to any code injector.
I recommend adding a distance check between the two players on Server:SoundToClient to prevent exploiting as well.
I recommend adding a distance check between the player and the coords on Server:SoundToCoords.
Other than that, nice to see a choice other than InteractSound. Would be nice to support more than .ogg
The code below works for .wav files… I’m not sure if it supports more than that.

var audio = new Audio(event.data.sound)
audio.volume = event.data.volume;
audio.play()
2 Likes

@ATG
Hi,
Thank you for your comments.

In regards to the Server:SoundToAll - This will be investigated and I will look to either remove this function or provide a version without this. Thank you for bringing up this important point.

In regards to the distance check on Server:SoundToClient - I will add this shortly and push this to GitHub.
In regards to the distance check on Server:SoundsToCoords - There is currently a distance check to see if the player is in the area, however I gather you must mean another layer of checking, if so, please specify as I will implement this to improve the security of the code.

Regarding the .wav files, this will be tested and implemented if working, I was not aware functionality for these events were provided. The code provided is already used in the index.html file and therefore should support .wav after testing.

Once again,
Thank you for supporting London Studios and helping us to improve the efficiency and security of the resource. The changes you have suggested are beneficial and will be implemented.

Best wishes,
London Studios.

1 Like

Love it!

Is it also possible to add awc audio? To change the type of siren

Hi,
Currently it is not possible to add AWC audio however this is something we will investigate in the future and look to add.
We hope you like the release.

Best wishes,
London Studios.

Hi,
Thank you for the comment.

If you have any suggestions or feedback please let us know.

Best wishes,
London Studios.

1 Like

Thanks for sharing this awesome script, What you think add on this script youtube video in background iframe ?

a Cool sound based system

Hi,
Thank you for your comment.

That would be very much possible yes.

Best wishes,
London Studios.

Hi,
Thank you for your comment.

If you have any suggestions please do not hesitate to let us know.

Best wishes,
London Studios

So just a heads up, i did some testing with this yesterday and the sound to coords and radius distancing does not work. Sound will play fine but moving away from it does not change the volume based on the distance from it.

@AGNetwork
Hi,
Once the sound is played that distance is set until that sound is finishing playing. This is due to the sounds being played through FiveM NUI, which uses AudioPlayer.

For example, you may be 16m away from the sound source. This will set the volume and then play it at that volume. If the sound was on a loop, the next time the sound plays you may find yourself closer and it will be louder. Whilst the same sound file is playing, if you start walking away the volume will not change.

Hope this makes sense,
Best wishes,
London Studios.

Furthermore,
If you still encounter any errors please respond with a small screenshot of your code in which you triggered the server event. Please ensure the values are passed correctly, eg float.

Best wishes,
London Studios.

Ah i get you now. And no errors i just thought it would work off distance from the coords. I do not do any c# but should’t you just be able to get the players distance every second or so and update the volume param accordingly? if dist > 2 then vol = 0.5 and so on

@AGNetwork
Hi,
This is something I have been investigating already as I do feel that would be a lot better.

Essentially this code sends an NUI message to play the sound, and the NUI handles the rest. I will look into communication with the NUI and Client resource file through the tick function to achieve the correct volume. Thanks for your comments.

Best wishes,
London Studios.