FRAMEWORKS
Frameworks

How to Make a Usable Item in QBCore With CreateUseableItem

November 29, 2024 · 4 min read

Creating usable items in your QBCore-based FiveM server can significantly enhance the gameplay experience for your players. With the CreateUseableItem function, you can craft items that players can dynamically use, whether they are food items, tools, or miscellaneous objects. In this guide, I will walk you through the steps on how to make a usable item in QBCore with CreateUseableItem.

Understanding Usable Items in QBCore

Usable items in QBCore are entities that players can interact with, such as healing kits, food, and drinks. By using the CreateUseableItem function, you can define how these items behave when used by players. This kind of functionality adds depth to the roleplay experience as players can utilize items in various ways that suit the server’s narrative.

Step-by-Step Guide to Creating a Usable Item

Follow these concrete steps to create your first usable item:

1. Set Up Your Resource Directory

Before anything else, ensure that you have created a resource directory for your usable item. This can be done within your server's resources folder, e.g., resources/[my_resources]/my_usable_item/.

2. Create the fxmanifest.lua File

Inside your my_usable_item directory, create a file named fxmanifest.lua. This file will contain the metadata required for your resource. Here’s an example of what the content should look like:

fx_version 'cerulean'
game 'gta5'

author 'YourName'
description 'A simple usable item'
version '1.0'

client_script 'client.lua'
server_script 'server.lua'

3. Create the client.lua File

Next, create a file named client.lua in the same directory. This file is responsible for handling the client-side logic when the item is used. Here’s a simple example:

RegisterNetEvent('my_usable_item:useItem')
AddEventHandler('my_usable_item:useItem', function()
    local playerPed = PlayerPedId()
    -- Animation (if needed)
    TaskStartScenarioInPlace(playerPed, 'WORLD_HUMAN_EATING', 0, true)
    -- Wait for the animation
    Citizen.Wait(3000)
    -- Show notification
    QBCore.Functions.Notify('You used a burger!')
    -- Add any effects here (like healing or hunger reduction)
end)

4. Create the server.lua File

Now create a server.lua file in the same directory. This will handle the server-side logic, including registering the item. Here’s an example to get you started:

QBCore.Functions.CreateUseableItem('burger', function(source)
    TriggerClientEvent('my_usable_item:useItem', source)
end)

5. Adding Your Usable Item to the Database

To register your usable item in the QBCore database, you might need to add it to your items table. Here’s an example SQL statement you can use:

INSERT INTO items (name, label, weight, type) VALUES ('burger', 'Burger', 1, 'item')

6. Testing Your Usable Item

Once you’ve set up your files and database, it’s time to test the functionality:

  1. Start your server and ensure there are no errors in the console.
  2. Spawn the item using an item spawn command or give yourself the item from your admin panel.
  3. Use the item in-game and check for notifications and effects.

Common Issues & Troubleshooting

While implementing usable items, you might encounter some issues. Here are some common problems and their solutions:

1. Item Not Usable

  • Check the Item Registration: Ensure the item is correctly registered in the database.
  • Verify the Trigger Event: Double-check the event name in both client.lua and server.lua for typos.

2. No Animation Playing

  • Check Animation Compatibility: Ensure the animation used in client.lua is available and correct.
  • Animation Timing: Adjust the Citizen.Wait duration if the animation flickers or does not play properly.

Enhancing Your Usable Items

Once you have the basic structure down, consider enhancing your usable items with additional features such as:

  • Custom Animations: Use different animations for different items.
  • HUD Elements: Display item effects such as health or hunger on the HUD.
  • Multiplayer Functionality: Allow items to affect other players when used, fostering interaction.

Frequently Asked Questions

What is the purpose of CreateUseableItem?

CreateUseableItem allows you to define how items behave when used in the QBCore framework, enhancing gameplay interactivity.

Can I add more effects to my items?

Yes! You can modify your items to have multiple effects, such as healing, buffing, or debuffing players when used.

Is this method applicable to ESX?

This specific method is tailored for QBCore; however, ESX has its own functions for creating usable items, which are similar but require different coding.

How do I troubleshoot item usage issues?

Check the console for error messages, verify item registration, and ensure event names match across your scripts.

Where can I find more QBCore resources?

Visit our scripts section for additional QBCore scripts to enhance your server.

#qbcore#fivem#usable items#createuseableitem#game development

Keep reading