7 min read
Sound in FiveM Scripts: Native Audio, Streamed Files and Making Something Play for the Right People
Sound in FiveM Scripts: Native Audio, Streamed Files and Making Something Play for the Right People
You add a satisfying little click to your new menu. It plays. You are delighted. You push it to the test server, a friend opens the menu, and hears absolutely nothing. You open it on your machine standing next to them and they hear that instead, faintly, from the wrong direction. Congratulations, you have discovered that FiveM script sound is client side and that nobody told you which client.
Audio is one of those parts of FiveM that is easy for five minutes and then suddenly involves file formats you have never met. This is the map: the built-in sounds you get for free, how to ship your own audio, when to give up and use NUI, and how to make a sound play for a group of people without shouting it at the entire server.
The free stuff: frontend sounds
GTA V ships with a large library of interface sounds, and you can play any of them with no assets and no setup. The frontend call takes a sound name and a sound set, and it plays flat in the player’s ears with no position in the world:
PlaySoundFrontend(-1, "SELECT", "HUD_FRONTEND_DEFAULT_SOUNDSET", true)
PlaySoundFrontend(-1, "CANCEL", "HUD_FRONTEND_DEFAULT_SOUNDSET", true)
PlaySoundFrontend(-1, "CHECKPOINT_PERFECT", "HUD_MINI_GAME_SOUNDSET", true)
The minus one is a sound id, and passing it means “I do not intend to stop this, fire and forget”. That is right for a click and wrong for anything that loops. There are community lists of sound names and sets floating about, and the reliable way to explore is to try them: a bad name fails silently rather than erroring, which is annoying to debug and very hard to break.
For a menu, a notification, a confirmation, a failure buzz, this is where you should stop. You get sounds that already match the game’s mix, that respect the player’s audio settings, and that cost you nothing in download size. Reaching for custom audio to make a button go beep is how a fifteen kilobyte resource becomes a ninety megabyte one.
Sound ids, looping, and the leak nobody notices
When you need a sound you can stop, ask the engine for an id first, use it, then hand it back:
local id = GetSoundId()
PlaySoundFromEntity(id, "Alarm_Loop", entity, "SOUNDSET_NAME", false, 0)
-- later
StopSound(id)
ReleaseSoundId(id)
The pool of sound ids is finite. Take them and never release them and you will eventually get to a state where nothing on that client plays any sound at all, which presents as “audio randomly stopped working after two hours” and gets blamed on everything except the script that did it. Release in the same place you stop, and release on resource stop too, because a developer restarting your resource forty times in an evening is the fastest way to find this bug.
You also have positional variants. Playing from an entity attaches the sound to that entity so it moves and attenuates correctly, and playing from a coordinate pins it to a spot in the world. Those two are what you want for anything diegetic: a machine humming, an alarm on a building, a radio in a shop.
Shipping your own audio, properly
When the game genuinely does not have the sound you need, you package your own. The format is Rockstar’s, which means an audio wave pack containing your audio, plus data files that describe the sounds and how they can be played. In your fxmanifest you declare the files and register them as audio data:
files {
'audiodirectory/**.awc',
'audioname_sounds.dat',
'audioname_game.dat',
}
data_file 'AUDIO_WAVEPACK' 'audiodirectory'
data_file 'AUDIO_SOUNDDATA' 'audioname_sounds.dat'
data_file 'AUDIO_GAMEDATA' 'audioname_game.dat'
Before you can play from a custom bank you have to load it, and loading is asynchronous. Request the audio bank, wait until the request succeeds, then play. Skipping the wait is the classic reason a custom sound plays on the second attempt and not the first, because by the second attempt the bank had quietly finished loading in the background.
Release banks when you are done with them. They occupy memory that the game would rather spend on other things, and a resource that holds four banks open forever is a resource that contributes to somebody else’s mystery crash.
Every byte of this ships to every player on first connect, which puts it in the same budget as your maps and your cars. Custom audio is not usually the biggest thing in a server’s download, but it is very often the least examined, in the same way vehicle packs quietly grow until somebody actually measures. Everything you stream lands in the same place and the player sees one number: how long they sat on the loading screen. That number is worth understanding properly, and how FiveM streaming actually works is the same for audio as it is for models.
The NUI shortcut, and its real costs
Because every FiveM resource can have an HTML page, you can play audio the way a website does: an audio element in an NUI page, told what to play by a message from Lua. Most of the popular general purpose sound resources work exactly this way, which is how they manage to play a file from a URL without any Rockstar audio packaging at all.
It is genuinely useful. Any format the browser supports, no bank loading, streaming from a remote URL, and volume control per sound. Three catches, and they are real:
- NUI audio does not respect the player’s in-game audio sliders. Somebody who has turned the game down to talk to their friends will still get your notification chime at full volume, which is how a nice touch becomes the reason they mute the server.
- There is no positional audio for free. Anything spatial you want, you calculate: distance from the source, adjust volume yourself, update it as people move. The popular sound resources do this for you and it costs a tick.
- The NUI page has to be loaded. If your audio lives in a resource whose UI is not running, nothing plays and nothing errors.
Rule of thumb: use NUI when the content is arbitrary, user supplied or streamed from elsewhere, like a jukebox or a radio someone can put a URL into. Use packaged game audio when it is part of the world.
Making it play for the right people
Here is the bit that trips up nearly everyone. Every one of these calls runs on a client and affects only that client. A server-side event that plays a sound does not exist. What you actually do is have the server tell some clients to play something.
The lazy version broadcasts to everyone and lets each client decide whether they are close enough to care. That works and it is fine at twenty players. At a hundred and fifty players with an alarm going off every few minutes, you are sending every event to every client so that most of them can throw it away, which shows up as network noise nobody can trace.
The better shape is to work out who is near the source on the server, using the coordinates you already track, and send only to them. It costs a little server-side work and it scales properly. Send the position with the event so the client can attach the sound to the right place rather than playing it flat in their head.
The other half is not trusting the client. A client event that says “play this sound at these coordinates” and is exposed to anyone with a mod menu becomes an air raid siren over the whole city about four minutes after a cheater notices it. Sound events want the same server side validation as anything else that a client can trigger, which puts them squarely inside your server access control thinking rather than off in the fun-features corner.
Mixing, and the reason your sound is annoying
A sound heard once is charming. The same sound heard four hundred times in a shift is a reason to uninstall. Some restraint that costs almost nothing:
- Put a cooldown on anything triggered by a repeatable action. Two clicks within a hundred milliseconds should not stack.
- Vary repeated sounds slightly. Even small pitch variation stops something sounding mechanical.
- Give players a volume setting, and honour it, especially for anything on the NUI path where the game sliders do nothing.
- Keep custom audio quiet relative to the game. Anything mastered louder than GTA’s own mix sounds broken rather than impactful.
The same applies to anything that comes with its own audio bundled in. Custom animation and emote packs often ship sounds along with the movement, and they are worth auditing for the same reasons, which the animation and emote side of asset work covers from the other direction.
The short version
Start with frontend sounds, because they are free and they already fit. Move to positional native audio when the sound belongs to a thing in the world. Package your own only when the game genuinely has not got it, and load and release the bank properly. Use NUI for arbitrary or streamed audio and accept that you own the volume problem. And always send sound events to the people who should hear them, not to everyone, which is also the answer to why your friend heard your menu click.