# Customs functions

## CheckOnlineCops (shared/utils.lua)

> This function returns the number of police officers (depending on the list of police jobs) on duty. Customize it if your server in another way.

```lua
function CheckOnlineCops()
    local copsOnline = 0
    for key, job in pairs(Config.utility.policeJobs) do
        if GlobalState[job] then
            copsOnline += GlobalState[job]
        end
    end
    return copsOnline
end
```

***

## Dispatch (client/dispatch.lua)

> Replaced the export with either the event or the export of your dispatch script

```lua
function CustomDispatch(coords)
    exports["ps-dispatch-esx"]:DrugSale(coords)
end
```

***

## Inventory (server/framework/framework.lua)

{% hint style="danger" %}
Firstly, before modifying, be sure of what you are doing. \
\
In all functions you will see "xPlayerSource" know that this represents the "player source" depending on your framework (esx or qbcore)\
\
so you don't need to touch it !
{% endhint %}

> By default the script uses ox\_inventory, it's up to you to adapt the script according to yours but while keeping the parameters intact.

### PlayerHasDrugs

```lua
function PlayerHasDrugs(xPlayerSource)
    local totalItems = 0
    local items = {}

    if xPlayerSource then
        items = exports.ox_inventory:Search(xPlayerSource, "count", Config.Drugs.list, nil)
        for name, count in pairs(items) do
            totalItems += count
        end
        return items, totalItems        
    end
end
```

> Here we check in the player's inventory if he has any drugs (depends on the list in "Config.Drugs.list") and return the total number of items and the items with their metadata

### RemoveDrugsItem

```lua
function RemoveDrugsItem(xPlayerSource, drugItem, amount)
    if xPlayerSource then
        exports.ox_inventory:RemoveItem(xPlayerSource, drugItem, amount)
    end
end
```

> Simply remove the drug that has just been sold from the player's inventory.

### AddRewardPlayer

```lua
function AddRewardPlayer(xPlayerSource, priceToPlayer)
    if xPlayerSource then
        exports.ox_inventory:AddItem(xPlayerSource, Config.Drugs.reward, priceToPlayer)
    end
end
```

> Give the reward to the player

### SendNotification

```lua
function SendNotification(xPlayerSource, type, msgInfo) -- type = "success" or "error"
    if xPlayerSource then
        if type == "success" then
            TriggerClientEvent("ox_lib:notify", xPlayerSource, {type = "success", 
            title = locale("Title_EndSale"), 
            description = msgInfo, 
            duration = 5000})
        else
            TriggerClientEvent("ox_lib:notify", xPlayerSource, {type = 'error', 
            title = locale("Title_Negotiate_failed"), 
            description = locale("Desc_Negotiate_failed"),
            position = "center-right",
            duration = 3500})
        end
    end
end
```

> By default we use ox\_lib to make notifications, it's up to you to change for your own notifications


---

# 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://dawsmo.gitbook.io/dawsmo-documentation/paid-scripts/drug-selling/customs-functions.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.
