# Stress Relief Items (/docs/orbit-studios-resources/orbit-dynamichud-stress/stress-relief-items)



# Stress Relief Items [#stress-relief-items]

Stress relief items lower the replicated `stress` state value that DynamicHUD reads. The examples below show where the item logic belongs for each inventory or framework style.

<Callout type="warn" title="ox_inventory implementation">
  The `ox_inventory` example uses the same server-side item export pattern as the GitBook docs. Put the export in `ox_inventory/modules/items/server.lua`, not in a custom client export.
</Callout>

<Tabs items="['ox_inventory', 'QB usable item', 'ESX usable item']">
  <Tab value="ox_inventory">
    Add the item handler in `ox_inventory/modules/items/server.lua`:

    ```lua title="ox_inventory/modules/items/server.lua"
    exports('cigarette', function(event, item, inventory, slot, data)
        local src = inventory.id

        -- Player is attempting to use the item
        if event == 'usingItem' then
            local currentStress = Player(src)?.state?.stress or 0
            local relief = math.random(5, 10)
            local newStress = math.max(currentStress - relief, 0)

            Player(src)?.state:set("stress", newStress, true)
        end

        -- Player finished using the item
        if event == 'usedItem' then
            TriggerClientEvent('ox_lib:notify', src, {
                description = 'You feel better already'
            })
        end
    end)
    ```

    Then define the usable item in `ox_inventory/data/items.lua`:

    ```lua title="ox_inventory/data/items.lua"
    cigarette = {
        label = "Cigarette",
        weight = 10,
        stack = true,
        consume = 1
    }
    ```

    `usingItem` runs when the player starts using the item. The handler reads the current stress state, removes a random amount from 5 to 10, clamps the result so it never goes below 0, and replicates the new value with `Player(src)?.state:set("stress", newStress, true)`.

    `usedItem` runs after the item finishes consuming. The GitBook implementation uses this event to show an `ox_lib` notification.

    To change the relief amount, only change the `relief` line:

    ```lua title="ox_inventory/modules/items/server.lua"
    local relief = 10
    ```

    ```lua title="ox_inventory/modules/items/server.lua"
    local relief = math.random(5, 10)
    ```

    ```lua title="ox_inventory/modules/items/server.lua"
    local relief = math.random(20, 30)
    ```
  </Tab>

  <Tab value="QB usable item">
    Use this pattern when your server uses QBCore usable items instead of `ox_inventory` item exports. Put the server callback in any started server resource, then trigger the stress resource server event with the amount you want to remove.

    ```lua title="resources/[custom]/server/main.lua"
    QBCore.Functions.CreateUseableItem('stress_relief', function(source)
        TriggerClientEvent('my_resource:client:relieveStress', source, 20)
    end)
    ```

    ```lua title="resources/[custom]/client/main.lua"
    RegisterNetEvent('my_resource:client:relieveStress', function(amount)
        TriggerServerEvent('hud:server:RelieveStress', amount)
    end)
    ```

    Add the item to your QB inventory item list using the same item name, `stress_relief`. If the inventory item name does not match the usable item name, the callback will never run.
  </Tab>

  <Tab value="ESX usable item">
    Use this pattern when your server uses ESX usable items. The ESX callback runs on the server, sends a small client event to the player, and the client asks the stress addon to remove stress.

    ```lua title="resources/[custom]/server/main.lua"
    ESX.RegisterUsableItem('stress_relief', function(source)
        TriggerClientEvent('my_resource:client:relieveStress', source, 20)
    end)
    ```

    ```lua title="resources/[custom]/client/main.lua"
    RegisterNetEvent('my_resource:client:relieveStress', function(amount)
        TriggerServerEvent('hud:server:RelieveStress', amount)
    end)
    ```

    Add the `stress_relief` item to your ESX inventory or item database before testing the usable item callback.
  </Tab>
</Tabs>
