# Events callbacks

## MOH Business System - Events & Callbacks

## Events & Callbacks

All event names use the resource folder name as a prefix, resolved automatically at runtime via `GetCurrentResourceName()`. In the examples below, `RES` represents this prefix (e.g. `moh-businesssystem`).

### Client → Server Events

| Event Name         | Parameters                              | Description                                                                                                                                                                                    |
| ------------------ | --------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `RES:buyItems`     | `storeId (string)`                      | Triggered when the player interacts with the supplier NPC during a mission. The server verifies the player has an active mission for this store, deducts payment, and adds items to the stash. |
| `RES:buyFromStore` | `storeId (string)`, `itemName (string)` | Triggered when a buyer clicks an item in the auto-sell store modal. The server checks stock, deducts payment from the buyer, gives the item, and pays the store owner.                         |

### Server → Client Events

| Event Name              | Parameters                                                          | Description                                                                                                            |
| ----------------------- | ------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| `RES:spawnSupplierNPC`  | `storeId (string)`, `npcIndex (number)`                             | Tells the client to spawn the supplier NPC at the selected mission location and set a waypoint.                        |
| `RES:deleteSupplierNPC` | none                                                                | Tells the client to delete the active supplier NPC after the mission is completed or cancelled.                        |
| `RES:spawnAutoSellNPC`  | `storeId (string)`, `ownerCid (string)`, `stashItems (table)`       | Broadcast to all clients. Tells them to spawn the auto-sell NPC for the given store so any player nearby can see it.   |
| `RES:deleteAutoSellNPC` | `storeId (string)`                                                  | Broadcast to all clients. Tells them to remove the auto-sell NPC for the given store.                                  |
| `RES:openStoreUI`       | `storeId (string)`, `ownerName (string)`, `items (table)`           | Sent to the buyer who interacted with the auto-sell NPC. Opens the store purchase modal with current stock and prices. |
| `RES:openStashUI`       | `storeId (string)`, `playerInventory (table)`, `stashItems (table)` | Opens the stash management modal for the store owner with their inventory and current stash contents.                  |
| `RES:updateStashUI`     | `playerInventory (table)`, `stashItems (table)`                     | Refreshes the stash management modal after a transfer without closing and reopening the UI.                            |

### NUI Callbacks (JS → Lua)

These are registered with `RegisterNUICallback` in the client script. The NUI posts to `https://RES/callbackName`.

| Callback Name       | Payload Fields                                   | Description                                                                                   |
| ------------------- | ------------------------------------------------ | --------------------------------------------------------------------------------------------- |
| `confirmOrder`      | `storeId`, `items` (array of `{item, quantity}`) | Submits the item order. Triggers a server event to validate and start the mission.            |
| `confirmPassword`   | `storeId`, `npcIndex`, `password`                | Sends the entered password to the server for verification or initial creation.                |
| `resetPassword`     | `storeId`, `npcIndex`                            | Deletes the stash password from the database.                                                 |
| `toggleAutoSell`    | `storeId`, `active` (boolean)                    | Activates or deactivates auto-sell for the store. Triggers NPC spawn or removal broadcast.    |
| `openStash`         | `storeId`                                        | Requests the server to send the stash and inventory data needed to open the management modal. |
| `transferToStash`   | `storeId`, `itemName`, `amount`                  | Moves the specified quantity of an item from the player's inventory into the store stash.     |
| `transferFromStash` | `storeId`, `itemName`, `amount`                  | Moves the specified quantity of an item from the store stash into the player's inventory.     |
| `setItemPrice`      | `storeId`, `itemName`, `price`                   | Saves a custom auto-sell price for an item to the database.                                   |
| `closeUI`           | none                                             | Signals Lua that the UI was closed. Re-enables cursor and player movement controls.           |

### Target Interaction Names

These are the internal names used when registering target zones on NPCs. They follow a predictable pattern based on the resource name and store ID.

| Pattern                | NPC           | Action                               |
| ---------------------- | ------------- | ------------------------------------ |
| `RES_order_{storeId}`  | Boss NPC      | Opens the shop UI for the owner      |
| `RES_browse_{storeId}` | Auto-sell NPC | Opens the buyer store modal          |
| `RES_buy_supplier`     | Supplier NPC  | Completes the current mission pickup |


---

# 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-businesssystem/events-callbacks.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.
