FiveM scripting – Our Guide (How To)

Get 20% Free Discount, With Code: SAVEMONEY

Tutorial: FiveM scripting in LUA

You want to write new scripts/programs for your FiveM server (or others)?
Every new beginning can be a challenge, but it’s definitely worth it to learn how to write code for FiveM. FiveM scripting isn’t that hard.

Good to know: FiveM uses LUA code for both client and server. For UI, it is good to use HTML, JS and CSS.

Setting up a server on FiveM

The easiest way to set up a FiveM server is to download a server template from ESX-Scripts.

  1. Create a new directory (for example D:\FXServer\server), this will be your server folder
  2. Download the last server version of FiveM: artifacts server.
  3. Extract the files
  4. Download and extract cfx-server-data to a folder, for example to  D:\FXServer\server-data.
  5. Create a server.cfg file inside the directory: server-data (this is an example: example server.cfg).
  6. Generate a new FiveM key from the keymaster: https://keymaster.fivem.net.
  7. Adjust the key inside the server.cfg: sv_licenseKey "licensekey".
  8. Start the server! Done.

Write own scripts

If you want to write own scripts, the best method is to look through already existing scripts. This is the best way to learn new ways of scripting and programming for FiveM servers.

However, here is an exciting video tutorial from YouTuber “Jeva” showing on how to create your first resource:

Resources

Resources are a collection of files that can be started, stopped, and restarted individually. A script is saved in a “resource”. For example: There is a resource for your cardealer, a resource for a weapon script, etc.

If you’re working on your own FiveM script, you’ve probably created a resource / [local] directory – this is ignored by Git when it updates the server data master. There we create a folder resources / [local] / GameMode, since we are creating a game type with the Mapmanager system. That means that we now need to have a folder like this, assuming a Windows development system: C: \ your \ path \ to \ cfx-server data \ resources \ [local] \ gamemode.

A manifest must be recognized by FiveM for a resource folder. Since this is a game type, some additional information is required to inform Mapmanager that it is a game type. Create a file in your GameMode folder called fxmanifest.lua (this is always Lua, even if you later write scripts in C # / JS).

txAdmin

txAdmin is a full featured web panel to Manage & Monitor your FiveM/RedM Server remotely, in use by over 10000 servers worldwide at any given time. Any FiveM server has txAdmin pre-installed!

Need help with txAdmin? Check out their github page.

FiveM scripting tutorial

FiveM supports the general purpose programming language Lua as one of its scripting languages. Lua is a very easy language to learn, simple to use and fast to write. To use Lua, just use .lua in your scripts file extensions.

Using Lua

To use Lua in your scripts, simply use the .lua file extension. No additional configuration is required.

Compile-time hashes

Because you might often have to deal with ‘Jenkins one-at-a-time’ hashes in GTA/RAGE, the Lua runtime has been extended to have support for compile-time generation of hash keys, similar to GET_HASH_KEY, however with zero runtime overhead.

For example:

-- getting
RequestModel(`adder`)

-- comparing
if GetEntityModel(vehicle) == `buzzard` then
  print("Indeed, it's a Buzzard.")
end

-- printing
print(`a_m_y_skater_01`)

Vectors & quaternions

FiveM supports first-class vectors and quaternions in Lua. Vectors and quaternions are incredibly useful to represent things like positions, rotations or even colors. For performance reasons, vectors and quaternions are real data types in CfxLua, just like booleans, numbers and strings are.

Many native functions return and accept vectors too. They’re commonly used for world positions and standard euler rotations, so use them whenever you can. More about there here.

For usage and examples see their respective docs:

Using natives

Native commands are mapped to global JavaScript functions with names similar to the Lua natives mapping. Definitions for them are located at path\to\fivem\FiveM.app\citizen\scripting\v8\natives_universal.d.ts.

For automatic completion of the FiveM API methods and mapped native functions we recommend using the official NPM packages @citizenfx/client and @citizenfx/server, respectively.

Example:

// The native command GET_PLAYER_PED translates to the following:
GetPlayerPed(-1);

Using Node.js APIs

FiveM includes a customized version of Node.js 16.x on the server. You can simply use require in server scripts, and it’ll resolve the package either from Node.js built-ins, or the node_modules/ folder in your resource directory.

To automatically install and update a package.json with Yarn on launch, make sure the yarn resource is running before starting your resource, or preferably add it as a dependency in your resource manifest.

Frequently asked questions

Well, all new things are hard. But don’t get frustrated too fast. Just keep on going. You might fail at first but it’s definitely worth it.

We recommend using the ESX framework to begin with. It is easy to understand and has the most downloadable addons (DLC) for it.

The best scripts are offered here on ESX-Scripts.

You can also check out the official FiveM forum for new development tips and resources.

Find the reason behind it! Check out your server console and download the error log via txAdmin.

Download ready-to-go scripts

Scripting is too hard for you? Check out the items in our shop to find scripts or maps for your FiveM server. Make your server more special today and start downloading from ESX Scripts!

Shopping Cart