# Functions/Events

### client/editable-main.lua

### Utility Functions

#### `isdead(QB)`

Checks if the player is dead or in a "last stand" state.

* If the QB framework is active, it checks the player's metadata for `isdead` or `inlaststand`.
* If the QB framework is not active, it checks the `LocalPlayer` state for the `isDead` condition.

#### `noBleed(QB)`

Simulates the use of a painkiller and applies the effect to prevent bleeding.

* If the QB framework is active, it uses the `PainKillerLoop` function from the `qb-am`

#### `crossNotify(QB, MSG)`

Displays a notification to the player, based on the framework used (either QB or ESX).

* If the QB framework is active, it uses `QB.Functions.Notify` to show the message.
* If the ESX framework is active, it uses `ESX.ShowNotification` to display the message.

### Pill Consumption & Effect Simulation

#### `PlayConsumeEffect()`

Simulates the effects of consuming a pill, including visual and animation effects like shaking and a stun.

* Requests the animation dictionary for the pill consumption and plays it.
* Waits for the animation to complete before applying additional effects.
* Plays a stun animation using the `ragdoll@human` dictionary, followed by applying a visual "drugged" effect (shaking camera and timecycle modifier).
* Applies ragdoll effects to simulate a stun after the pill consumption.
* Waits for a specified duration (4 seconds) before clearing the ragdoll and resetting visual effects.

### server/editable-main.lua

### Event Handlers

#### `pill:removeItem`

This event is used to remove an item from the player's inventory, based on the configured inventory system.

```lua
RegisterNetEvent('pill:removeItem', function(player, itemName)
    if Config.inventory == "qb" then
        -- QB-Core Inventory
        local xPlayer = QBCore.Functions.GetPlayer(player)
        if xPlayer then
            xPlayer.Functions.RemoveItem(itemName, 1)
        end
    elseif Config.inventory == "qs" then
        -- QS Inventory
        exports['qs-inventory']:RemoveItem(player, itemName, 1)
    elseif Config.inventory == "ps" then
        -- PS Inventory
        exports['ps-inventory']:RemoveItem(player, itemName, 1)
    elseif Config.inventory == "origen" then
        -- Origen Inventory
        exports.origen_inventory:RemoveItem(player, itemName, 1)
    end
end)
```

#### `RegisterPillAsUsable`

This function registers a pill as a usable item in the player's inventory.

```lua
function RegisterPillAsUsable(itemName)
    if Config.inventory == "qb" then
        -- QB-Core Inventory
        QBCore.Functions.CreateUseableItem(itemName, function(source, item)
            TriggerEvent('pill:consume', itemName, source)
        end)
    elseif Config.inventory == "qs" then
        -- QS Inventory
        exports['qs-inventory']:CreateUsableItem(itemName, function(source, item)
            TriggerEvent('pill:consume', itemName, source)
        end)
    elseif Config.inventory == "ps" then
        -- PS Inventory
        exports['ps-inventory']:CreateUsableItem(itemName, function(source)
            TriggerEvent('pill:consume', itemName, source)
        end)
    elseif Config.inventory == "origen" then
        -- Origen Inventory
        exports.origen_inventory:CreateUseableItem(itemName, function(source)
            TriggerEvent('pill:consume', itemName, source)
        end)
    end
end
```

This function registers the item as usable for each supported inventory system and triggers the `pill:consume` event when the item is used.

#### `onResourceStart`

This event handler registers pills as usable when the resource starts.

```lua
AddEventHandler('onResourceStart', function(resourceName)
    if GetCurrentResourceName() == resourceName then
        for powerName, powerData in pairs(Config.power) do
            if powerData.itemName then
                RegisterPillAsUsable(powerData.itemName)
            end
        end

        for key, value in pairs(Config.Items) do
            RegisterPillAsUsable(value)
        end
    end
end)
```

When the resource starts, the script checks the `Config.power` and `Config.Items` tables to register each pill item as usable.

### Admin Commands

#### `/givepower` (QBCore and ESX)

This command allows an administrator to give a power to a player.

**QBCore Command**

```lua
QBCore.Commands.Add("givepower", "Give power to a player", {{name="playerId", help="Player ID"}, {name="power", help="Power name"}}, false, function(source, args)
    local playerId = tonumber(args[1])
    local powerName = args[2]
    local src = source
    -- Check if the user is an admin
    if QBCore.Functions.HasPermission(src, 'command') then
        if playerId and powerName then
            -- Check if the player is online
            local targetPlayer = QBCore.Functions.GetPlayer(playerId)
            if targetPlayer then
                -- Trigger the client event to give power
                TriggerClientEvent("pill:consume", playerId, powerName, true)
                TriggerClientEvent('QBCore:Notify', src, string.format("Admin %s gave power for player ID %d.", GetPlayerName(src), playerId))
            else
                TriggerClientEvent('QBCore:Notify', src, "Player not found or not online.", "error")
            end
        else
            TriggerClientEvent('QBCore:Notify', src, "Invalid player ID or missing power name.", "error")
        end
    else
        TriggerClientEvent('QBCore:Notify', src, "You do not have permission to use this command.", "error")
    end
end)
```

**ESX Command**

```lua
RegisterCommand('givepower', function(source, args, rawCommand)
    local playerId = tonumber(args[1])
    local xPlayer = ESX.GetPlayerFromId(source)
    -- Check if the user is an admin
    if xPlayer.getGroup() == 'admin' then
        if playerId then
            -- Check if the player is online
            local targetPlayer = ESX.GetPlayerFromId(playerId)
            if targetPlayer then
                -- Trigger the client event to give power
                local powerName = args[2]
                if powerName then
                    TriggerClientEvent("pill:consume", targetPlayer.source, powerName, true)
                    TriggerClientEvent('esx:showNotification', source, (string.format("Admin %s gave power for player ID %d.", xPlayer.identifier, playerId)))
                else
                    TriggerClientEvent('esx:showNotification', source, "Missing Power Name")
                end
            else
                TriggerClientEvent('esx:showNotification', source,"Player not found or not online.")
            end
        else
            TriggerClientEvent('esx:showNotification', source,"Invalid player ID.")
        end
    else
        TriggerClientEvent('esx:showNotification', source,"You do not have permission to use this command.")
    end
end, false)
```

#### `/clearpowers` (QBCore and ESX)

This command allows an administrator to remove all powers from a player.

**QBCore Command**

<pre class="language-lua"><code class="lang-lua"><strong>QBCore.Commands.Add("clearpowers", "Clear powers for a player", { { name = "id", help = "Player ID" } }, true, function(source, args)
</strong>    local playerId = tonumber(args[1]) -- Get player ID from arguments
    local src = source

    -- Check if the user is an admin
    if QBCore.Functions.HasPermission(src, 'command') then
        if playerId then
            -- Check if the player is online
            local targetPlayer = QBCore.Functions.GetPlayer(playerId)
            if targetPlayer then
                -- Trigger the client event to clear powers
                TriggerClientEvent('pill:removeAllPowers', targetPlayer.PlayerData.source) 
                TriggerClientEvent('QBCore:Notify', src, string.format("Admin %s cleared powers for player ID %d.", GetPlayerName(src), playerId))
            else
                TriggerClientEvent('QBCore:Notify', src, "Player not found or not online.", "error")
            end
        else
            TriggerClientEvent('QBCore:Notify', src, "Invalid player ID.", "error")
        end
    else
        TriggerClientEvent('QBCore:Notify', src, "You do not have permission to use this command.", "error")
    end
end)
</code></pre>

**ESX Command**

```lua
RegisterCommand('clearpowers', function(source, args, rawCommand)
    local playerId = tonumber(args[1]) -- Get player ID from arguments
    local xPlayer = ESX.GetPlayerFromId(source)
    -- Check if the user is an admin
    if xPlayer.getGroup() == 'admin' then
        if playerId then
            -- Check if the player is online
            local targetPlayer = ESX.GetPlayerFromId(playerId)
            if targetPlayer then
                -- Trigger the client event to clear powers
                TriggerClientEvent('pill:removeAllPowers', targetPlayer.source) 
                TriggerClientEvent('esx:showNotification', targetPlayer.source, (string.format("Admin %s cleared powers for player ID %d.", xPlayer.identifier, playerId)))
            else
                TriggerClientEvent('esx:showNotification', source,"Player not found or not online.")
            end
        else
            TriggerClientEvent('esx:showNotification', source,"Invalid player ID.")
        end
    else
        TriggerClientEvent('esx:showNotification', source,"You do not have permission to use this command.")
    end
end, false)
```


---

# 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://enyo-scripts.gitbook.io/documentations/fivem-scripts/superpowers/functions-events.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.
