GUIDES
Guides & Tutorials

How to Make Items Usable with ox_inventory in QBCore

March 3, 2024 · 4 min read

Unlocking the full potential of your QBCore server involves creating an engaging player experience, and making items usable using ox_inventory is a crucial step. This guide walks you through how to make items usable with ox_inventory in QBCore, allowing your players to interact with their inventories dynamically.

Understanding ox_inventory Basics

Before diving into configuration, it’s essential to understand what ox_inventory is and how it fits within the QBCore framework. ox_inventory is an advanced inventory system designed to streamline item management, providing a seamless experience for players.

Key Features of ox_inventory

  • Customizable Item Usability: Easily define how items can be used within the game.
  • Drag and Drop Interface: Users can manage their inventory intuitively.
  • Database Support: Items are saved to the database for persistence.
  • Server Events: Supports custom server events for advanced functionalities.

Installing and Configuring ox_inventory

To make items usable, ensure that ox_inventory is properly installed and configured in your QBCore server.

Installation Steps

  1. Download ox_inventory: Obtain the latest version from the repository.
  2. Add to Resources: Place the downloaded folder in your server's resources directory.
  3. Update fxmanifest.lua: Add the following lines to your fxmanifest.lua for ox_inventory:
    dependency 'ox_lib'
    client_scripts {
        'client/cl_inventory.lua',
        'client/cl_usability.lua'
    }
    server_scripts {
        'server/sv_inventory.lua'
    }
    
  4. Start Resource: Ensure that your server.cfg has the line start ox_inventory.

Configuring Usable Items

After installation, you’ll need to configure the items you’d like to make usable. This is located in the config.lua file within the ox_inventory directory.

Example Configuration Steps

  1. Locate the Config File: Open ox_inventory/config.lua.
  2. Define Usable Items: Scroll down to the items section and add the items you want to be usable. Here’s an example:
    Items = {
        ['water'] = {
            usable = true,
            onUse = function() 
                -- Custom behavior when item is used
            end,
        },
        ['medkit'] = {
            usable = true,
            onUse = function(playerId)
                TriggerClientEvent('medkit:use', playerId)
            end,
        },
    },
    
  3. Define Item Behavior: The onUse function is where you specify what happens when a player uses the item. You can implement specific functions like recovering health with medkit or creating a drink effect with water.

Creating Usable Items: A Practical Example

To provide clarity, let’s walk through a practical example of making a health kit usable within your server.

Step-by-Step Implementation

  1. Define the Health Kit Item: In the config.lua file, add:
    ['healthkit'] = {
        usable = true,
        onUse = function(playerId)
            local player = QBCore.Functions.GetPlayer(playerId)
            if player.Functions.RemoveItem('healthkit', 1) then
                player.Functions.AddHealth(50)
                TriggerClientEvent('QBCore:Notify', playerId, 'You used a health kit!', 'success')
            end
        end,
    },
    
  2. Test the Functionality: Load your server and test using the health kit in-game. Ensure players receive notifications and health is incremented correctly.

Troubleshooting Common Issues

While setting up and configuring ox_inventory, you may encounter a few common issues. Here’s how to troubleshoot them:

Top Troubleshooting Tips

  • Item Not Usable: Check if the item is correctly defined in config.lua. Ensure that usable is set to true.
  • Server Crashes: Review server logs for errors. Check if all dependencies are correctly installed and loaded before ox_inventory.
  • Function Not Executing: Ensure that your function for onUse is correctly referencing the player ID. Debug with print() statements to check if the code executes as expected.

Advanced Usable Item Features

For those looking to enhance user experience further, consider these advanced features:

  • Multiple Item Usability: You can create combined items, such as a sandwich which requires bread and cheese.
  • Conditional Usability: Make items usable only under certain conditions (e.g., a health item that cannot be used in combat).
  • Custom Events: Trigger custom client-side events to create unique interactions when items are used.

Frequently Asked Questions

Q1: Can I use ox_inventory with ESX?
No, ox_inventory is specifically designed for QBCore; however, similar functionalities may exist in ESX with different implementations.

Q2: How do I change item images in ox_inventory?
Images can usually be set in the item definition within the config.lua. Look for entries specifying an image path.

Q3: Is it possible to use items without a specific action?
Yes, you can set usable = false in the item definition and utilize other methods to interact with items without triggering onUse.

Q4: What are some examples of usable items I can configure?
Common examples include food items (e.g., burgers, water), health kits, and tools for crafting or interacting with environments.

Q5: Where can I find more resources on QBCore?
For more scripts and resources to enhance your QBCore server, check out our extensive collection on Fivemania's scripts page.

By following this guide on how to make items usable with ox_inventory in QBCore, you can significantly improve the interactivity of your server, leading to a more engaging player experience. Happy scripting!

#fivem#qbc#ox_inventory#guide#scripts

Keep reading