Sounds
Trigger sounds for players from server scripts.
PlaySoundForSource
Play a sound for a specific player.
exports['koja-lib']:PlaySoundForSource(source, file, volume, soundId, loop)
| Parameter | Type | Description |
|---|---|---|
source | number | Player server ID |
file | string | Short sound name or full NUI URL |
volume | number? | Volume 0.0 – 1.0 (default: 0.5) |
soundId | string? | ID for stopping later |
loop | boolean? | Loop the sound (default: false) |
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)
| Parameter | Type | Description |
|---|---|---|
coords | vector3 | Origin of the sound |
dist | number | Maximum hearing radius (max 250 m) |
file | string | Short sound name or full NUI URL |
volume | number? | Base volume at origin |
soundId | string? | ID for stopping later |
loop | boolean? | Loop |
StopSoundForAll
Stop a looping sound for all players.
exports['koja-lib']:StopSoundForAll(soundId)
Examples
Play alert for one player
exports['koja-lib']:PlaySoundForSource(source, 'alert', 0.8)
Looping heist alarm for everyone
exports['koja-lib']:PlaySoundForAll('alarm', 0.6, 'heistAlarm', true)
-- Stop after 30 seconds
SetTimeout(30000, function()
exports['koja-lib']:StopSoundForAll('heistAlarm')
end)
Distance-based explosion from another resource
-- server script in my-heist
local origin = GetEntityCoords(GetPlayerPed(source))
exports['koja-lib']:PlayDistanceSound(
origin,
80.0,
'https://cfx-nui-my-heist/sounds/explosion.mp3',
1.0
)
Using sounds from another resource
The other resource just needs the file declared in its fxmanifest.lua:
-- my-script/fxmanifest.lua
files {
'sounds/siren.mp3',
}
Then trigger with the full NUI URL:
exports['koja-lib']:PlaySoundForAll(
'https://cfx-nui-my-script/sounds/siren.mp3',
0.7,
'siren',
true
)
Net Events (alternative)
-- Play for the triggering player (client → server)
TriggerServerEvent('koja-lib:server:onlySourceSound', file, volume, soundId, loop)
-- Play distance sound from the triggering player's position
TriggerServerEvent('koja-lib:server:distanceSound', dist, file, volume, soundId, loop)
-- Stop sound for all
TriggerServerEvent('koja-lib:server:stopSound', soundId)
Related pages
- Police — Functions for checking how many police officers are online.
- Callbacks — The callback system allows server scripts to handle requests from clients and vice versa.
- Licenses — Check whether a player holds a specific driving or skill license.
- Money — Functions for reading and modifying a player's money on the server.
- Inventory — Server-side inventory functions.
- Notifications — Send notifications to a player from the server side.
- Server API — back to the section overview