# Sounds

Trigger sounds for players from server scripts.

:::hint{type="info"}
All sounds are played through koja-lib's NUI page. See [Client Sounds](/koja-lib/api-reference/client-api/sounds) for details on file sources and NUI URLs.
:::

## PlaySoundForSource

Play a sound for a specific player.

```lua
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.

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

## PlayDistanceSound

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

```lua
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.

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

## Examples

### Play alert for one player

```lua
exports['koja-lib']:PlaySoundForSource(source, 'alert', 0.8)
```

### Looping heist alarm for everyone

```lua
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

```lua
-- 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`:

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

Then trigger with the full NUI URL:

```lua
exports['koja-lib']:PlaySoundForAll(
    'https://cfx-nui-my-script/sounds/siren.mp3',
    0.7,
    'siren',
    true
)
```

## Net Events (alternative)

```lua
-- 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](/koja-lib/api-reference/server-api/police) — Functions for checking how many police officers are online.
- [Callbacks](/koja-lib/api-reference/server-api/callbacks) — The callback system allows server scripts to handle requests from clients and vice versa.
- [Licenses](/koja-lib/api-reference/server-api/licenses) — Check whether a player holds a specific driving or skill license.
- [Money](/koja-lib/api-reference/server-api/money) — Functions for reading and modifying a player's money on the server.
- [Inventory](/koja-lib/api-reference/server-api/inventory) — Server-side inventory functions.
- [Notifications](/koja-lib/api-reference/server-api/notifications) — Send notifications to a player from the server side.
- [Server API](/koja-lib/api-reference/server-api) — back to the section overview
