> For the complete documentation index, see [llms.txt](https://docs.kojascripts.eu/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kojascripts.eu/koja-lib/api-reference/server-api/sounds.md).

# Sounds

Trigger sounds for players from server scripts.

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

## 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)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.kojascripts.eu/koja-lib/api-reference/server-api/sounds.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
