
This guide aims to introduce readers to LUA scripting within the context of FiveM, a popular modification framework for GTA V. We will cover the essentials of LUA, how it integrates with FiveM, and the steps required to set up a development environment using Visual Studio Code (VSCode). By the end of this post, readers will have a working development environment and a basic understanding of LUA scripting for FiveM.
Is LUA hard to learn? Well, every beginning is difficult – but LUA is a very easy coding language.
LUA is a lightweight, high-level programming language designed for embedded use in applications. It is known for its simplicity, ease of integration, and fast execution. Originally developed in 1993, LUA has been widely adopted in various domains, particularly in game development, due to its flexibility and minimalistic syntax.
Why Use LUA in FiveM?
In the context of FiveM, LUA is the primary scripting language used to interact with the game engine. It allows developers to write scripts that can:
The FiveM code provides a rich set of native functions and event handlers that can be accessed via LUA scripts, making it possible to extend and customize almost every aspect of the game.
FiveM is a multiplayer modification framework for GTA V, enabling players to connect to dedicated servers with customized game modes and content. Unlike the standard multiplayer mode of GTA V, FiveM allows server owners to use mods, scripts, and custom assets to create unique experiences for players.
FiveM’s LUA Scripting Capabilities Include:
With FiveM, developers can create custom game modes such as roleplay servers, racing competitions, and mini-games. LUA serves as the backbone for these scripts, providing the logic and control needed to interact with the game world and players.
To start scripting with LUA in FiveM, you need to set up a suitable development environment. This section will guide you through the process of installing Visual Studio Code, configuring it for LUA development, and setting up a basic FiveM server environment to test your scripts.

Visual Studio Code is a free, open-source code editor developed by Microsoft. It’s lightweight, feature-rich, and supports various programming languages, including LUA.
Ctrl+Shift+X.Search for “Lua” and install an extension such as “Lua Language Server” or “Lua Plus.” These extensions provide syntax highlighting, code completion, and other useful features.# Sample command to install Lua extension from the VSCode marketplace ext install sumneko.lua
To write and test LUA scripts, you need a local FiveM server setup. Follow these steps to create your development environment:
C:\FiveMServer).resources. This is where your custom scripts and resources will reside.# Basic FiveM server configuration file # Server name and description sv_hostname "My FiveM Development Server" sv_description "A development server for testing LUA scripts" # Maximum number of players sv_maxclients 32 # Resource directories ensure mapmanager ensure chat ensure spawnmanager ensure sessionmanager ensure fivem ensure hardcap ensure rconlog # Add custom resources here ensure my_script
Then, lastly, Start the Server.
resources folder, create a new directory for your script, e.g., my_script.my_script, create two files: __resource.lua (or better: Start by setting up a fxmanifest.lua) and main.lua.__resource.lua file (or fxmanifest) is a metadata file that tells FiveM about the resources in this directory. Add the following lines to __resource.lua:-- __resource.lua -- Define the server script to run server_script 'main.lua'
In main.lua, write a simple LUA script that outputs a message to the server console:
-- main.lua
print("Hello, FiveM! This is my first LUA script.")
Test Your Script:
LUA’s syntax is designed to be simple and clean. Understanding the basics is essential for effective scripting in FiveM.
Comments:
--.--[[ ]].-- This is a single-line comment --[[ This is a multi-line comment ]]
Variables and Data Types:
local playerName = "John" -- string local playerScore = 100 -- number local isOnline = true -- boolean
Basic Data Types:
true or false.Example of Variable Declaration and Data Types in LUA:
local playerHealth = 100 -- Number
local playerName = "Alex" -- String
local isAlive = true -- Boolean
local playerInfo = { -- Table
name = "Alex",
health = 100,
inventory = {}
}
Print Function: The print() function outputs messages to the console. It’s useful for debugging and displaying information.
<code>print("This message will be printed to the server console")<br></code>
Server Event Handlers: FiveM uses event-driven programming. You can define functions that run in response to specific events, such as a player joining the server.
-- Example of an event handler in FiveM LUA
AddEventHandler('playerConnecting', function(playerName, setKickReason)
print(playerName .. " is connecting to the server")
end)
x, use playerHealth or currentScore. This improves readability and maintainability:local playerHealth = 100 -- Better naming for clarity
if playerHealth > 0 then
print("Player is alive")
else
print("Player is dead")
end
-- Check if the player is alive
if playerHealth > 0 then
print("Player is alive")
end
Using pcall for Error Handling:
pcall stands for “protected call.” It executes a function in protected mode and catches any errors.local success, err = pcall(function()
-- Some code that might throw an error
print("Executing risky code")
error("An error occurred!")
end)
if not success then
print("Error caught: " .. err)
end
Debugging Tips:
print() statements to check the values of variables at different stages.Let’s create a simple chat command that players can use to greet each other. This example will illustrate how to handle player input and respond with a custom message.
main.lua file to include the following code:-- Register a chat command /greet
RegisterCommand('greet', function(source, args, rawCommand)
local playerName = GetPlayerName(source)
if playerName then
print(playerName .. " used the greet command.")
TriggerClientEvent('chat:addMessage', source, {
args = { "Server", "Hello " .. playerName .. ", welcome to the server!" }
})
else
print("Command used by unknown player.")
end
end, false)
/greet. When a player types this command in the chat, the server responds with a greeting message./greet.As your scripts become more complex, organizing your project files becomes critical. Here are some tips for managing your FiveM LUA scripts effectively:
Directory Structure: Keep related scripts in separate folders within the resources directory. For example, create separate folders for player-related scripts, vehicle scripts, and UI scripts:
/resources
├── my_script
│ ├── __resource.lua
│ ├── main.lua
│ └── commands.lua
├── player_management
│ ├── __resource.lua
│ ├── player_health.lua
│ └── player_inventory.lua
└── vehicle_management
├── __resource.lua
├── vehicle_spawn.lua
└── vehicle_control.lua
Modular Scripting: Break your scripts into smaller modules that handle specific tasks. This makes it easier to maintain and update individual components without affecting the entire script.
Use a Version Control System: Consider using Git to track changes to your scripts. This allows you to roll back to previous versions if something breaks and collaborate with other developers more effectively.
Documentation: Document your code thoroughly. Create a README file in each project directory explaining what each script does, how to use it, and any dependencies it may have.

Read our FiveM guide about error fixing
Now that you have set up a basic development environment and written your first LUA script, you can explore more advanced topics in subsequent blog posts. These will include:
Setting up a development environment for FiveM LUA scripting using VSCode is the first step towards creating rich, customized gameplay experiences. With this foundational setup, you can begin writing scripts to enhance your FiveM server. The example scripts and exercises provided here are just the beginning. As you gain more experience with LUA and FiveM, you’ll be able to create more complex and engaging scripts, pushing the boundaries of what’s possible in GTA V.
/greet command to accept an additional parameter. If a name is provided, greet that name; otherwise, greet the player who used the command./shout that broadcasts a message to all players on the server.