Sounds

koja-lib plays audio through its NUI page. Sounds can come from koja-lib's own sounds/ folder or from any other resource.


Playing a Sound (Client Export)

Play a sound for the local player directly from another resource's client script:

exports['koja-lib']:PlaySound(file, volume, soundId, loop)
Parameter
Type
Description

file

string

Short name or full NUI URL (see below)

volume

number?

Volume 0.0 – 1.0 (default: 0.5)

soundId

string?

Unique ID for stopping the sound later

loop

boolean?

Whether to loop (default: false)


Stopping a Sound (Client Export)

exports['koja-lib']:StopSound(soundId)
Parameter
Type
Description

soundId

string

The ID used when playing the sound


Sound File Sources

Sounds inside koja-lib

Place .mp3 files in web/build/sounds/ and reference them by name (without extension):

Sounds from another resource

Add the .mp3 to the other resource's files {} block in its fxmanifest.lua:

Then pass the full NUI URL:

FiveM serves every file listed in files {} at https://cfx-nui-<resourcename>/<path>.


Server Exports

Trigger sounds for players from a server script:

PlaySoundForSource

Play a sound for a specific player.

PlaySoundForAll

Play a sound for every connected player.

PlayDistanceSound

Play a sound for all players within dist metres of coords. Volume fades with distance.

Parameter
Type
Description

coords

vector3

Origin coordinates

dist

number

Maximum hearing distance (max 250)

file

string

Short name or full NUI URL

volume

number?

Base volume at the origin

soundId

string?

ID for later stop

loop

boolean?

Loop

StopSoundForAll

Stop a looping sound for every player.


Examples

Looping alarm that can be stopped

Distance-based explosion sound from another resource

Client: one-shot UI sound


Net Events (alternative)

If you prefer events over exports:

Last updated