
Introduction to LUA-Scripting for FiveM
August 29, 2024
Zuletzt aktualisiert: February 24, 2026
2 Min. Lesezeit
text
1
2
3
# Sample command to install Lua extension from the VSCode marketplace
<p>ext install sumneko.lua</p>
text
1
2
3
4
5
6
7
8
9
# Basic FiveM server configuration file
<p># Server name and description sv_hostname My FiveM Development Server sv_description A development server for testing LUA scripts</p>
<p># Maximum number of players sv_maxclients 32</p>
<p># Resource directories ensure mapmanager ensure chat ensure spawnmanager ensure sessionmanager ensure fivem ensure hardcap ensure rconlog</p>
<p># Add custom resources here ensure my_script</p>
text
1
2
3
-- __resource.lua
<p>-- Define the server script to run server_script 'main.lua'</p>
text
1
2
3
-- main.lua
<p>print(Hello, FiveM! This is my first LUA script.)</p>
text
1
2
3
-- This is a single-line comment
<p>--[[ This is a multi-line comment ]]</p>
text
1
2
3
local playerName = John -- string
<p>local playerScore = 100 -- number local isOnline = true -- boolean</p>
text
1
2
3
local playerHealth = 100 -- Number
<p>local playerName = Alex -- String local isAlive = true -- Boolean local playerInfo = { -- Table name = Alex, health = 100, inventory = {} }</p>
text
1
<code>print(This message will be printed to the server console)<br></code>
text
1
2
3
-- Example of an event handler in FiveM LUA
<p>AddEventHandler('playerConnecting', function(playerName, setKickReason) print(playerName .. is connecting to the server) end)</p>
text
1
local playerHealth = 100 -- Better naming for clarity
text
1
2
3
if playerHealth > 0 then
<p>print(Player is alive) else print(Player is dead) end</p>
text
1
2
3
-- Check if the player is alive
<p>if playerHealth > 0 then print(Player is alive) end</p>
text
1
2
3
4
5
local success, err = pcall(function()
<p>-- Some code that might throw an error print(Executing risky code) error(An error occurred!) end)</p>
<p>if not success then print(Error caught: .. err) end</p>
text
1
2
3
-- Register a chat command /greet
<p>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)</p>
text
1
2
3
/resources
<p>├── 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</p>
Bleib auf dem Laufenden
Erhalte die neuesten FiveM-Tutorials, Mod-Releases und exklusive Updates direkt in dein Postfach.
Kein Spam. Jederzeit abbestellbar.
Artikel teilen