Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

here is a server.lua a fivem script here is the code make with this code a SQL database

  1. User

    here is a server.lua a fivem script here is the code make with this code a SQL database

  2. Assistant

    To create an SQL database for a FiveM script in Lua, you’ll typically utilize a database management system like MySQL or SQLite. Below, I'll outline how to connect to a MySQL database from your FiveM server using the mysql-async library, which is commonly used with FiveM.

    Step 1: Install mysql-async

    First, you need to ensure that you have the mysql-async resource properly set up in your FiveM server. You can find it here.

    1. Download or clone the mysql-async repository from GitHub.
    2. Place it in your server's resources folder.
    3. Add start mysql-async to your server configuration file (server.cfg).

    Step 2: Create the Database

    Next, you’ll create a MySQL database. You can do this through a MySQL management tool like phpMyAdmin or command line. Here’s an example SQL statement to create a simple table:

    CREATE DATABASE fivem_db;
    
    USE fivem_db;
    
    CREATE TABLE users (
        id INT AUTO_INCREMENT PRIMARY KEY,
        identifier VARCHAR(255) UNIQUE NOT NULL,
        name VARCHAR(255) NOT NULL,
        money INT NOT NULL DEFAULT 0
    );
    

    Step 3: Connect to the Database in Your server.lua

    Here’s an example server.lua file using the mysql-async library to interact with the database you just created:

    -- server.lua
    
    -- Ensure mysql-async is loaded
    local MySQL = require('mysql-async')
    
    -- Ensure the script connects to the database upon startup
    MySQL.ready(function ()
        print("MySQL Database connected successfully!")
    end)
    
    -- Function to create a new user (insert into the database)
    RegisterServerEvent('createUser')
    AddEventHandler('createUser', function(identifier, name)
        local _source = source
        MySQL.Async.execute('INSERT INTO users (identifier, name) VALUES (@identifier, @name)', {
            ['@identifier'] = identifier,
            ['@name'] = name
        }, function(rowsChanged)
            if rowsChanged > 0 then
                TriggerClientEvent('notify', _source, 'User created successfully!')
            else
                TriggerClientEvent('notify', _source, 'User already exists or error occurred.')
            end
        end)
    end)
    
    -- Function to retrieve user data
    RegisterServerEvent('getUserData')
    AddEventHandler('getUserData', function(identifier)
        local _source = source
        MySQL.Async.fetchAll('SELECT * FROM users WHERE identifier = @identifier', {
            ['@identifier'] = identifier
        }, function(result)
            if #result > 0 then
                TriggerClientEvent('sendUserData', _source, result[1])
            else
                TriggerClientEvent('notify', _source, 'User not found.')
            end
        end)
    end)
    
    -- Example of a Thread to Update Player Money
    RegisterCommand('setMoney', function(source, args, rawCommand)
        local identifier = args[1] -- identifier of the player
        local amount = tonumber(args[2]) -- amount to set
    
        if identifier and amount then
            MySQL.Async.execute('UPDATE users SET money = @money WHERE identifier = @identifier', {
                ['@money'] = amount,
                ['@identifier'] = identifier
            }, function(rowsChanged)
                if rowsChanged > 0 then
                    TriggerClientEvent('notify', source, 'Money updated successfully!')
                else
                    TriggerClientEvent('notify', source, 'Error updating money.')
                end
            end)
        else
            TriggerClientEvent('notify', source, 'Invalid parameters.')
        end
    end, false)
    

    Notes:

    1. Ensure you replace the database connection details in the mysql-async configuration if needed.
    2. This code includes a way to create a user and retrieve user data from the database.
    3. The notify event should be handled on the client-side for notifications to the player.
    4. Ensure you have error handling and security measures in place for a production environment, especially when handling user data.

    Final Step: Start Your Server

    Make sure to restart your server to apply the changes and test your implementation. You should now have a functional SQL database interaction in your FiveM server script!