Koja Scripts
KOJA-LIB

Sounds

koja-lib plays audio through its NUI page.

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)
ParameterTypeDescription
filestringShort name or full NUI URL (see below)
volumenumber?Volume 0.0 – 1.0 (default: 0.5)
soundIdstring?Unique ID for stopping the sound later
loopboolean?Whether to loop (default: false)

Stopping a Sound (Client Export)

exports['koja-lib']:StopSound(soundId)
ParameterTypeDescription
soundIdstringThe 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):

exports['koja-lib']:PlaySound('alert')           -- plays koja-lib/web/build/sounds/alert.mp3
exports['koja-lib']:PlaySound('alert', 0.8, 'myAlert', false)

Sounds from another resource

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

-- my-script/fxmanifest.lua
files {
    'sounds/alarm.mp3',
}

Then pass the full NUI URL:

exports['koja-lib']:PlaySound('https://cfx-nui-my-script/sounds/alarm.mp3', 0.8, 'alarm')

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.

exports['koja-lib']:PlaySoundForSource(source, file, volume, soundId, loop)

PlaySoundForAll

Play a sound for every connected player.

exports['koja-lib']:PlaySoundForAll(file, volume, soundId, loop)

PlayDistanceSound

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

exports['koja-lib']:PlayDistanceSound(coords, dist, file, volume, soundId, loop)
ParameterTypeDescription
coordsvector3Origin coordinates
distnumberMaximum hearing distance (max 250)
filestringShort name or full NUI URL
volumenumber?Base volume at the origin
soundIdstring?ID for later stop
loopboolean?Loop

StopSoundForAll

Stop a looping sound for every player.

exports['koja-lib']:StopSoundForAll(soundId)

Examples

Looping alarm that can be stopped

-- Server: start alarm
exports['koja-lib']:PlaySoundForAll('alarm', 0.6, 'heistAlarm', true)

-- Server: stop alarm after 30 seconds
SetTimeout(30000, function()
    exports['koja-lib']:StopSoundForAll('heistAlarm')
end)

Distance-based explosion sound from another resource

-- server script in my-heist resource
local origin = GetEntityCoords(GetPlayerPed(source))
exports['koja-lib']:PlayDistanceSound(
    origin,
    80.0,
    'https://cfx-nui-my-heist/sounds/explosion.mp3',
    1.0
)

Client: one-shot UI sound

-- client script in my-shop resource
exports['koja-lib']:PlaySound('https://cfx-nui-my-shop/sounds/purchase.mp3', 0.4)

Net Events (alternative)

If you prefer events over exports:

-- Client: play for self
TriggerEvent('koja-lib:client:onlySourceSound', file, volume, soundId, loop)

-- Client: stop
TriggerEvent('koja-lib:client:stopSound', soundId)

-- Server: play for triggering player
TriggerServerEvent('koja-lib:server:onlySourceSound', file, volume, soundId, loop)

-- Server: play distance sound from player's position
TriggerServerEvent('koja-lib:server:distanceSound', dist, file, volume, soundId, loop)
  • Callbacks — koja-lib provides a callback system that lets client scripts request data from the server and vice versa.
  • Inventory — Client-side inventory functions let you check what items the local player has without a server round-trip.
  • DUI — DUI (Dynamic UI) allows you to render a web page as a texture on any in-game surface — screens, billboards, laptops, phones, etc.
  • Money — Retrieve the local player's money balance from the client side via a server callback.
  • Keybinds — Register persistent key bindings that players can rebind in the GTA V settings menu.
  • Notifications — koja-lib provides two notification interfaces: SendNotify (routes through the configured backend) and LibNotify (built-in koja-lib notification).
  • Client API — back to the section overview