# features

## Real-time passenger sync

When any player performs an action (play, pause, seek, volume change), the server validates the request and forwards it to every passenger in the vehicle. All occupants hear the same audio at the same position simultaneously.

## State persistence via statebags

Song state is stored directly on the vehicle entity using FiveM statebags:

| Key           | What it stores                          |
| ------------- | --------------------------------------- |
| `currentSong` | Full song data (URL, name, thumbnail)   |
| `currTime`    | Last known playback position in seconds |
| `isPaused`    | Whether playback is currently paused    |
| `volume`      | Current volume level                    |

Because statebags are shared across all clients automatically, any player who joins the vehicle mid-song will immediately receive the correct state without any additional server calls.

## Leave and rejoin handling

### When a player leaves a vehicle

* The current timestamp is saved to the statebag.
* Playback is paused locally on that client.
* Other passengers are unaffected.

### When a player re-enters the same vehicle

The script checks the statebag and handles two scenarios:

**Sound is still loaded locally** — the script resumes immediately from the saved position at the correct volume. No re-download needed.

**Sound was unloaded** (e.g. after a disconnect or long absence) — the script reloads the URL, then seeks to the saved timestamp automatically once it is ready.

## Volume persistence

Volume is stored on the vehicle statebag. Any passenger who joins later inherits the same volume level. Adjusting the slider updates the statebag immediately so new joiners always get the correct value.

## Server-side ownership validation

When a client sends a `moh_carplay:syncmusic` event, the server checks that the requesting player is actually inside the vehicle before forwarding it to anyone. Requests from players outside the vehicle are silently dropped, preventing spoofed events.

## Resource stop cleanup

When the resource stops or restarts, the script:

1. Kills all active update loops.
2. Destroys all spawned sounds via xsound.
3. Clears `currentSong`, `currTime`, and `isPaused` from every vehicle statebag it touched.

This prevents ghost sounds and stale statebag data after a `restart moh_carplay`.

## Multi-framework notifications

The `notify()` function automatically adapts to your framework:

| Framework  | Method used               |
| ---------- | ------------------------- |
| QBCore     | `QBCore.Functions.Notify` |
| ESX        | `ESX.ShowNotification`    |
| Standalone | Native GTA V feed ticker  |


---

# Agent Instructions: 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://mohssins.gitbook.io/mohscriptsdocs/moh-carplay/features.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.
