# esx_optionalneeds (/docs/addons/esx_optionalneeds)

`esx_optionalneeds` adds alcohol to ESX Legacy. It registers a `drunk` status in [esx_status](/docs/addons/esx_status) and a usable `beer` item. While the status is above zero the player walks drunk, gets motion blur and a screen filter; when it drops back to zero the effects fade out.

## Overview [#overview]

| Fact | Value |
| --- | --- |
| Resource | `esx_optionalneeds` |
| Side | Server (usable item) and client (status and effects) |
| Manifest dependencies | `esx_lib`, `es_extended` |
| Needs at runtime | [esx_status](/docs/addons/esx_status) (not listed in the manifest) |
| SQL | `esx_optionalneeds.sql` adds the `beer` item |

## Installation [#installation]

<Steps>
<Step>
### Import the item [#import-the-item]

```sql title="esx_optionalneeds.sql"
INSERT INTO `items` (`name`, `label`, `weight`) VALUES
	('beer', 'Beer', 1)
;
```

The `localization/` folder has translated variants. Some of them, such as `en_esx_optionalneeds.sql`, also insert beer into a `shops` table. Only use those if your shop resource uses that table.
</Step>
<Step>
### Start it after esx_status [#start-it-after-esx_status]

```bash title="server.cfg"
ensure esx_status
ensure esx_optionalneeds
```
</Step>
</Steps>

## Configuration [#configuration]

| Key | Default | Description |
| --- | --- | --- |
| `Config.TickTime` | `100` | Not used by the current code. |
| `Config.UpdateClientTime` | `5000` | Not used by the current code. |
| `Config.Locale` | `GetConvar('esx:locale', 'en')` | Language of the "you drank a beer" notification. |

The drunk values are hard-coded in `client/main.lua` and `server/main.lua`.

## How it works [#how-it-works]

1. Using `beer` removes one beer, adds `250000` to `drunk` on the client, plays a drinking scenario and shows a notification.
2. On `esx_status:loaded` the client registers `drunk` with default `0`, color `#8F15A5`, visible only while above zero, and a tick that removes `1500` per esx_status tick (1 second by default).
3. A loop checks the status every second and applies a drunk level:

| Drunk value | Level | Walk style |
| --- | --- | --- |
| 1 to 250000 | 0 | `move_m@drunk@slightlydrunk` |
| 250001 to 500000 | 1 | `move_m@drunk@moderatedrunk` |
| above 500000 | 2 | `move_m@drunk@verydrunk` |

Every level also sets the `spectator5` timecycle, motion blur and `SetPedIsDrunk`. When the value reaches `0`, the screen fades and everything is reset.

## Events [#events]

### esx_optionalneeds:onDrink (client, net) [#esx_optionalneedsondrink-client-net]

Plays the `WORLD_HUMAN_DRINKING` scenario for one second. Harmless if triggered by anyone.

```lua
TriggerClientEvent('esx_optionalneeds:onDrink', playerId)
```

### Making players drunk from your own script [#making-players-drunk-from-your-own-script]

`drunk` is a custom (client-owned) status, so use the client events of esx_status:

```lua title="server/main.lua"
ESX.RegisterUsableItem('vodka', function(source)
    local xPlayer = ESX.Player(source)
    xPlayer.removeInventoryItem('vodka', 1)

    TriggerClientEvent('esx_status:add', source, 'drunk', 400000)
    TriggerClientEvent('esx_optionalneeds:onDrink', source)
end)
```

```lua title="client/main.lua"
-- sober up instantly, for example after a hospital visit
TriggerEvent('esx_status:set', 'drunk', 0)
```

## Common issues [#common-issues]

- **Beer does nothing**: esx_status is not started, or started after esx_optionalneeds, so `drunk` was never registered.
- **"Item beer does not exist"**: the SQL was not imported, or you use an inventory with its own item list (see [ox_inventory](/docs/guides/ox-inventory)).
- **Effects stay after relog**: the drunk value is saved with the other custom statuses and continues after reconnect. It decays as usual.

## Related [#related]

- [esx_status](/docs/addons/esx_status)
- [esx_basicneeds](/docs/addons/esx_basicneeds)
- [Add items](/docs/guides/add-items)
