Advanced FiveM Scripting with ESX: Adding Items to the Database

In this tutorial, we’ll create an advanced script that interacts with the server-side and utilizes the ESX framework. Specifically, we’ll focus on adding new items to the database and using these items in-game. This guide is ideal for those who have basic knowledge of FiveM scripting and are ready to take on more complex tasks.

Prerequisites

Before you begin, ensure you have:

  • A legal copy of Grand Theft Auto V
  • Basic programming knowledge in Lua
  • A text editor (e.g., Visual Studio Code)
  • A working FiveM server with ESX installed
  • MySQL or MariaDB setup

Setting Up Your Environment

  1. Install ESX:
    Ensure ESX is installed on your server. You can follow the official ESX installation guide.
  2. Create the Script Folder:
    In your resources directory, create a new folder named esx_advanced_script.
  3. fxmanifest File:
    In your esx_advanced_script folder, create a file named fxmanifest.lua:
   fx_version 'cerulean'
   game 'gta5'

   author 'YourName'
   description 'ESX Advanced Script'
   version '1.0.0'

   shared_script '@es_extended/imports.lua'

   server_scripts {
       '@mysql-async/lib/MySQL.lua',
       'server.lua'
   }

   client_script 'client.lua'

Adding Items to the Database

  1. Database Setup:
    Open your MySQL or MariaDB database management tool (e.g., phpMyAdmin) and select your ESX database. Execute the following SQL query to add a new item to the items table:
   INSERT INTO items (name, label, weight, rare, can_remove) VALUES ('health_kit', 'Health Kit', 1, 0, 1);

This query adds a new item called health_kit to your database.

Creating the Server Script

  1. Server Script (server.lua):
   ESX = nil

   TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

   -- Register a usable item
   ESX.RegisterUsableItem('health_kit', function(source)
       local xPlayer = ESX.GetPlayerFromId(source)
       xPlayer.removeInventoryItem('health_kit', 1)
       TriggerClientEvent('esx_advanced_script:useHealthKit', source)
   end)

   -- Register a server-side command
   RegisterCommand('givekit', function(source, args, rawCommand)
       local xPlayer = ESX.GetPlayerFromId(source)
       xPlayer.addInventoryItem('health_kit', 1)
       TriggerClientEvent('chat:addMessage', source, {
           args = {"^1SYSTEM", "You have received a health kit!"}
       })
   end, false)

This script does the following:

  • Registers a usable item (health_kit) that, when used, triggers a client event.
  • Registers a server-side command (/givekit) to give the player a health kit.

Creating the Client Script

  1. Client Script (client.lua):
   RegisterNetEvent('esx_advanced_script:useHealthKit')
   AddEventHandler('esx_advanced_script:useHealthKit', function()
       local playerPed = PlayerPedId()
       local maxHealth = GetEntityMaxHealth(playerPed)
       SetEntityHealth(playerPed, maxHealth)
       TriggerEvent('chat:addMessage', {
           args = {"^2SYSTEM", "You have used a health kit and are now fully healed!"}
       })
   end)

This script listens for the useHealthKit event and fully heals the player when the event is triggered.

Configuring the Server

  1. Update server.cfg:
    Open your server.cfg file and add the following line to start your new resource:
   start esx_advanced_script

Testing the Script

  1. Start Your Server:
    Start your FiveM server and connect to it.
  2. Using the Script:
  • Give a Health Kit: Enter the command /givekit in the chat to receive a health kit.
  • Use the Health Kit: Open your inventory (depends on your ESX inventory system) and use the health kit to fully heal your character.

Best Practices and Tips

  • Error Handling: Always include error handling to manage unexpected situations gracefully.
  • Code Modularity: Keep your scripts modular to make them easier to manage and update.
  • Community Resources: Utilize community forums and documentation for ESX and FiveM to solve issues and get ideas for new features.

Conclusion

This advanced tutorial guides you through creating a more complex FiveM script that interacts with both the client and server-side, utilizing the ESX framework and adding new items to your database. By following these steps, you can create richer gameplay experiences and enhance your FiveM server’s functionality. Happy scripting!


With this guide, you should have a robust understanding of integrating server-side scripting with ESX in FiveM, including adding new items to the database. Continue exploring and expanding your scripts to create an engaging and dynamic server environment!

Leave a Reply

Your email address will not be published. Required fields are marked *

6 + 10 =