here is a server.lua a fivem script here is the code make with this code a SQL database
- User
here is a server.lua a fivem script here is the code make with this code a SQL database
- 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-asynclibrary, which is commonly used with FiveM.Step 1: Install mysql-async
First, you need to ensure that you have the
mysql-asyncresource properly set up in your FiveM server. You can find it here.- Download or clone the
mysql-asyncrepository from GitHub. - Place it in your server's resources folder.
- Add
start mysql-asyncto 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.luaHere’s an example
server.luafile using themysql-asynclibrary 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:
- Ensure you replace the database connection details in the
mysql-asyncconfiguration if needed. - This code includes a way to create a user and retrieve user data from the database.
- The
notifyevent should be handled on the client-side for notifications to the player. - 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!
- Download or clone the