This code is a server-side script written in Lua for...
July 5, 2025 at 10:32 PM
This code is a server-side script written in Lua for a FiveM roleplay server running the QBCore framework. Here is a breakdown of what it does:
1. Retrieve the QBCore Object
local QBCore = exports['qb-core']:GetCoreObject()
This line retrieves the core object of the qb-core
framework. It is used to access QBCore functionalities such as player data, commands, and exports.
2. Add a Command (/save
)
QBCore.Commands.Add("save", "Salva il veicolo e le modifiche", {}, false, function(source)
local src = source
TriggerClientEvent('qb-savevehicle:client:GetVehicleData', src)
end)
- This adds a new server-side command
/save
to QBCore's command system. - When a player enters
/save
in the game, the function triggers a client-side event namedqb-savevehicle:client:GetVehicleData
. This is sent to the player who executed the command (source
).
3. Server-Side Event: Save Vehicle Data
RegisterNetEvent('qb-savevehicle:server:SaveVehicleData', function(vehicleProps)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local citizenid = Player.PlayerData.citizenid
- This registers a server-side event named
qb-savevehicle:server:SaveVehicleData
. - It receives the vehicle's properties (
vehicleProps
, e.g., plate, modifications) as a parameter from the client. - The script retrieves the player data of the player who triggered the event (using
source
) and extracts their uniquecitizenid
.
4. Save or Update Vehicle Data in the Database
if vehicleProps and vehicleProps.plate and citizenid then
local plate = vehicleProps.plate
vehicleProps.model = GetEntityModel(NetworkGetEntityFromNetworkId(vehicleProps.netId or 0)) or vehicleProps.model
local props_json = json.encode(vehicleProps)
exports.oxmysql:insert('INSERT INTO saved_vehicles (citizenid, plate, vehicle_data) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE vehicle_data = ?', {
citizenid, plate, props_json, props_json
})
- It checks if the necessary vehicle data (
vehicleProps
withplate
and the player'scitizenid
) exists. - The vehicle's model is retrieved using its network ID or default model if not found.
- The vehicle properties are then JSON-encoded (
props_json
) for easy database storage. - Using the
oxmysql
export, it inserts the vehicle data into the database tablesaved_vehicles
. If a record with the sameplate
already exists for thatcitizenid
, it updates the existing one instead (ON DUPLICATE KEY UPDATE
clause).
5. Notify the Player of Success or Failure
TriggerClientEvent('QBCore:Notify', src, 'Veicolo salvato con successo!', 'success')
else
TriggerClientEvent('QBCore:Notify', src, 'Errore nel salvataggio del veicolo.', 'error')
end
end)
- If the saving is successful, the player is notified via the
QBCore:Notify
client-side event with a success message. - If there's an issue (e.g., missing data), the player receives an error notification.
Overall Functionality
This code adds functionality for players to save their vehicle's properties (like modifications, model, etc.) to the server's database using /save
. It interacts with the qb-core
framework to:
- Retrieve player data.
- Gather vehicle information.
- Save/update that information in the database.
- Notify the player of the result.
The data is stored in a table
saved_vehicles
with fields:
citizenid
(the player's unique identifier),plate
(vehicle license plate),- and
vehicle_data
(JSON-encoded vehicle properties).
Generate your own explanations
Download our vscode extension
Read other generated explanations
Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node