> ## Documentation Index
> Fetch the complete documentation index at: https://docs.jaksam-scripts.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Change or block a sale

> Set the price of a drug sale from your own script, or cancel it, before the player gets paid.

Every time drugs are sold, Drugs Creator asks your code first, right before the player gets paid. Your code can keep the price, set a new one or cancel the sale. It works for every selling method: NPC, corner, narcos, pushers, plane, boat and hired dealers.

It's the place to plug in your own economy: prices that change with the area, supply and demand, gang activity or the police on duty.

You can do it in two ways. Pick the one you prefer, or use both.

## The easy way: sv\_integrations.lua

At the bottom of `integrations/sv_integrations.lua` there is a commented example. Move the `BeforeDrugSale` function out of the comment and edit it:

```lua Function theme={null}
function BeforeDrugSale(sale)
    if sale.isHotZone then
        return sale.price * 2 -- double price in hot zones
    end
end
```

If you updated from an older version and kept your own `sv_integrations.lua`, just paste the function at the bottom of the file. Without it, sales work as before.

## From your own resource

If your economy lives in its own resource, register a hook from there:

```lua Export theme={null}
local hookId = exports.drugs_creator:registerSaleHook(function(sale)
    return sale.price + 100
end, { methods = { "npc", "corner" }, drugs = { "weed" } })
```

The second parameter is optional: it limits the hook to some selling methods and some drugs. Leave it out and the hook runs for every sale.

`exports.drugs_creator:removeSaleHook(hookId)` removes it. When your resource stops, its hooks are removed on their own.

> [!NOTE]
>
> Start your resource **after** `drugs_creator`. If you restart Drugs Creator, it forgets the hooks: register them again when it starts.
>
> ```lua theme={null}
> AddEventHandler("onResourceStart", function(resourceName)
>     if resourceName == "drugs_creator" then
>         -- register your hook again here
>     end
> end)
> ```

## What you return

| You return | What happens |
| - | - |
| nothing, or `true` | The price stays as it is |
| a number, like `500` | That is the new total price of the sale |
| `false` | The sale is cancelled, the player sees "The buyer changed their mind, the deal is off" |
| `{ cancel = true, message = "Market closed" }` | The sale is cancelled, the player sees your message |
| `{ price = 500 }` | Same as returning `500` |

When a sale is cancelled, the player keeps the drugs and gets no money. When you change the price, the notification, the Discord log, the dashboard statistics and the [selling events](https://docs.jaksam-scripts.com/drugs-creator/server/sellings) all use your price.

If more hooks are registered, they run one after the other: each one gets the price left by the one before.

## The sale

Your function receives one table, `sale`, with everything about the sale:

| Name | Data Type | Description |
| - | - | - |
| `method` | string | `npc`, `corner`, `narcos`, `pushers`, `plane`, `boat` or `hired_dealer` |
| `drug` | string | Item ID of the drug |
| `label` | string | Item label of the drug |
| `quantity` | integer | Quantity sold |
| `price` | integer | Total price the player would receive, low police reduction included |
| `basePrice` | integer | Total price before the low police reduction |
| `unitPrice` | integer | `price` divided by `quantity` |
| `rewardObject` | table | Account or item the player is paid with |
| `playerId` | integer | Server ID of the player who sells (for hired dealers: the owner, only when online) |
| `identifier` | string | Character identifier of the player (for hired dealers: the owner) |
| `job` | string | Job of the player (for hired dealers: the owner's job) |
| `gang` | string | Gang of the player, if your gang module has one |
| `coords` | vector3 | Where the player is |
| `territory` | string | Name of the territory where the sale happens, `nil` outside territories |
| `territoryOwner` | string | Who owns that territory |
| `isHotZone` | boolean | `true` when the territory is a hot zone |
| `preview` | boolean | `true` when the narcos menu is only showing the price, see below |
| `pedNetId` | integer | Network ID of the buyer (NPC and corner only) |
| `pusherId` | string | ID of the pusher (pushers only) |
| `dealer` | table | `id`, `name` and `type` of the hired dealer (hired dealers only) |

## Example

```lua theme={null}
-- Hypothetical market script: price from your own market, closed at night
function BeforeDrugSale(sale)
    local hour = os.date("*t").hour
    if hour >= 2 and hour < 6 then
        return { cancel = true, message = "Nobody is buying right now, come back later" }
    end

    local marketPrice = exports["my_market"]:getCachedPrice(sale.drug, sale.territory)
    if marketPrice then
        return marketPrice * sale.quantity
    end
end
```

> [!WARNING]
>
> **Return right away.** Don't use `Wait` or `MySQL.await` inside the hook: a hook that waits cancels the sale, and the server console says so. Keep your prices in memory and read them from there.

> [!NOTE]
>
> * If your hook throws an error or returns something that isn't a price, the sale goes on at the price it had, and the console shows a warning
> * **Narcos:** when a player opens the narcos menu, your hook is called with `preview = true` so the menu shows your price. It's called again with `preview = false` when the sale really happens: change your stock or supply only then
> * Plane and boat call the hook once for each drug: you can cancel one drug and let the others sell
> * A cancelled hired dealer sale keeps the drugs in the dealer's stock


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.