QB-Core Fundamentals
A guide to the basics of QB-Core development.
Introduction
QB-Core is a popular framework for FiveM that provides a solid foundation for building a roleplay server. This guide will introduce you to the fundamentals of QB-Core development.
File Structure
QB-Core scripts follow a standard file structure. Here's an overview of the most important files and directories:
- fxmanifest.lua: This file is the manifest for your script. It tells FiveM what files to load and what resources to use.
- config.lua: This file is used for configuring your script. It's where you'll put things like database credentials, API keys, and other settings.
- server.lua: This file contains the server-side code for your script.
- client.lua: This file contains the client-side code for your script.
- shared.lua: This file contains code that is shared between the server and the client.
Events
Events are a key part of FiveM scripting. They allow you to trigger actions and communicate between scripts. QB-Core has a number of built-in events that you can use, and you can also create your own custom events.
Server-Side Events
-- Trigger a client event from the server
TriggerClientEvent('my-event', source, 'hello from the server')
Client-Side Events
-- Trigger a server event from the client
TriggerServerEvent('my-event', 'hello from the client')
Functions
QB-Core provides a number of built-in functions that you can use in your scripts. These functions can help you with everything from getting player data to creating new items.
Getting Player Data
-- Get the player's character data
local PlayerData = QBCore.Functions.GetPlayerData()
-- Get the player's job
local job = PlayerData.job.name
The QBCore Shared Object
The QBCore shared object is a global object that is available on both the client and server. It contains a number of useful functions and variables that you can use in your scripts.
Key Functions
QBCore.Functions.GetPlayer(source): Returns the player object for the given source.QBCore.Functions.CreateCallback(name, cb): Creates a new callback that can be triggered from the client or server.QBCore.Functions.TriggerCallback(name, cb, ...): Triggers a callback and returns the result.QBCore.Functions.Notify(source, message, type): Shows a notification to the player.
The Player Object
The player object contains all the information about a player, such as their character data, job, and inventory. You can get the player object using the QBCore.Functions.GetPlayer function.
Key Properties
PlayerData.citizenid: The player's character ID.PlayerData.job: An object containing the player's job information.PlayerData.items: An array of the items in the player's inventory.
Callbacks
Callbacks are a way to get data from the server on the client, or from the client on the server. They are useful when you need to get information that is only available on one side.
Creating a Callback
-- In a server-side script
QBCore.Functions.CreateCallback('my-callback', function(source, cb)
-- Do something cool here
local data = 'hello from the server'
cb(data)
end)
Triggering a Callback
-- In a client-side script
QBCore.Functions.TriggerCallback('my-callback', function(data)
-- Do something with the data
print(data) -- prints "hello from the server"
end)
Conclusion
This is just a brief introduction to QB-Core development. There's much more to learn, but this should give you a good foundation to build upon.