{"id":151273,"date":"2024-08-29T08:59:09","date_gmt":"2024-08-29T06:59:09","guid":{"rendered":"https:\/\/hifivem.com\/?p=151273"},"modified":"2026-08-16T15:16:25","modified_gmt":"2026-08-16T13:16:25","slug":"introduzione-allo-scripting-lua","status":"publish","type":"post","link":"https:\/\/fivemx.com\/it\/introduzione-allo-scripting-lua\/","title":{"rendered":"Introduzione allo scripting Lua per FiveM"},"content":{"rendered":"<p><!-- fivemx-editorial-reindex:2026-08-16:151273 --><\/p>\n<p><strong>FiveM resources can use CfxLua, a modified Lua 5.4 runtime, for client and server scripts.<\/strong> A current resource starts with <code data-no-translation=\"\">fxmanifest.lua<\/code>, lists the files that each context loads, and keeps authoritative decisions such as permissions, money and inventory on the server.<\/p>\n<div class=\"fivemx-guide-box fivemx-guide-note\">\n<p>The old <code data-no-translation=\"\">__resource.lua<\/code> manifest and the <code data-no-translation=\"\">lua54 'yes'<\/code> switch are deprecated for new work. Cfx.re states that Lua 5.3 support ended in June 2025 and current scripts use Lua 5.4.<\/p>\n<\/div>\n<h2>Create a minimal resource<\/h2>\n<p>Inside your server-data <code data-no-translation=\"\">resources<\/code> directory, create <code data-no-translation=\"\">[local]\/hello_fivem<\/code>. The brackets group resources; the resource itself is the <code data-no-translation=\"\">hello_fivem<\/code> directory.<\/p>\n<p><code data-no-translation=\"\">fxmanifest.lua<\/code>:<\/p>\n<pre data-no-translation=\"\"><code data-no-translation=\"\" class=\"language-lua\">fx_version 'cerulean'\ngame 'gta5'\n\nauthor 'Your name'\ndescription 'Small CfxLua learning resource'\nversion '1.0.0'\n\nclient_script 'client.lua'\nserver_script 'server.lua'\n<\/code><\/pre>\n<p><code data-no-translation=\"\">client.lua<\/code>:<\/p>\n<pre data-no-translation=\"\"><code data-no-translation=\"\" class=\"language-lua\">RegisterCommand('mycoords', function()\n    local coords = GetEntityCoords(PlayerPedId())\n    print(('x=%.2f y=%.2f z=%.2f'):format(coords.x, coords.y, coords.z))\nend, false)\n<\/code><\/pre>\n<p><code data-no-translation=\"\">server.lua<\/code>:<\/p>\n<pre data-no-translation=\"\"><code data-no-translation=\"\" class=\"language-lua\">AddEventHandler('onResourceStart', function(resourceName)\n    if resourceName ~= GetCurrentResourceName() then\n        return\n    end\n\n    print(('[%s] started'):format(resourceName))\nend)\n<\/code><\/pre>\n<p>The manifest and runtime behaviour are documented in the official <a href=\"https:\/\/docs.fivem.net\/docs\/scripting-reference\/resource-manifest\/\" rel=\"nofollow noopener\" target=\"_blank\">resource manifest reference<\/a> and <a href=\"https:\/\/docs.fivem.net\/docs\/scripting-manual\/runtimes\/lua\/\" rel=\"nofollow noopener\" target=\"_blank\">CfxLua guide<\/a>.<\/p>\n<h2>Understand client and server contexts<\/h2>\n<p>A client script runs separately for each connected player and can call game-facing natives such as <code data-no-translation=\"\">PlayerPedId<\/code> and <code data-no-translation=\"\">GetEntityCoords<\/code>. A server script coordinates shared state, persistence and authorization. Values supplied by a client are inputs, not proof.<\/p>\n<p>Use <code data-no-translation=\"\">AddEventHandler<\/code> for events that should stay in one context. Use <code data-no-translation=\"\">RegisterNetEvent<\/code> only when an event must cross the client\/server boundary. The official <a href=\"https:\/\/docs.fivem.net\/docs\/developers\/server-security\/\" rel=\"nofollow noopener\" target=\"_blank\">Secure Your Events guide<\/a> explains why every networked action must validate the player&#8217;s current server-side permission and state.<\/p>\n<h2>Avoid blocking the scheduler<\/h2>\n<p>CfxLua scripts are cooperatively scheduled. A loop that never yields can freeze its script context. When a loop must wait, use <code data-no-translation=\"\">Wait<\/code> with a delay appropriate to the job, and do not poll a database or HTTP endpoint every frame. Prefer events and state changes over permanent polling.<\/p>\n<h2>Use exports for a clear resource boundary<\/h2>\n<p>An export exposes a named function to another resource. Define it explicitly and document its arguments and return value:<\/p>\n<pre data-no-translation=\"\"><code data-no-translation=\"\" class=\"language-lua\">exports('formatPlayerLabel', function(serverId, playerName)\n    return ('[%d] %s'):format(serverId, playerName)\nend)\n<\/code><\/pre>\n<p>Another Lua resource can call it as <code data-no-translation=\"\">exports.hello_fivem:formatPlayerLabel(id, name)<\/code>. Do not use an export to bypass permission or ownership checks.<\/p>\n<h2>Run and test the resource<\/h2>\n<ol>\n<li>Back up the resource you are replacing, if any.<\/li>\n<li>In the server console, run <code data-no-translation=\"\">refresh<\/code> and then <code data-no-translation=\"\">ensure hello_fivem<\/code>.<\/li>\n<li>Check the server console for the start message.<\/li>\n<li>Connect to the development server, open F8 and run <code data-no-translation=\"\">mycoords<\/code>.<\/li>\n<li>Restart the server to confirm the resource is included in the normal startup order.<\/li>\n<\/ol>\n<p>The supported resource commands are listed in the official <a href=\"https:\/\/docs.fivem.net\/docs\/server-manual\/server-commands\/\" rel=\"nofollow noopener\" target=\"_blank\">server-command reference<\/a>. Continue with the official <a href=\"https:\/\/docs.fivem.net\/docs\/scripting-manual\/introduction\/creating-your-first-script\/\" rel=\"nofollow noopener\" target=\"_blank\">first Lua script tutorial<\/a> before adding framework or database dependencies.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create a small FiveM resource with CfxLua 5.4 and fxmanifest.lua, understand client and server contexts, and validate networked actions on the server.<\/p>","protected":false},"author":1,"featured_media":151274,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2340],"tags":[3001],"class_list":["post-151273","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-lua-scripting","tag-fivem-script"],"_links":{"self":[{"href":"https:\/\/fivemx.com\/it\/wp-json\/wp\/v2\/posts\/151273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fivemx.com\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fivemx.com\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fivemx.com\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fivemx.com\/it\/wp-json\/wp\/v2\/comments?post=151273"}],"version-history":[{"count":3,"href":"https:\/\/fivemx.com\/it\/wp-json\/wp\/v2\/posts\/151273\/revisions"}],"predecessor-version":[{"id":217360,"href":"https:\/\/fivemx.com\/it\/wp-json\/wp\/v2\/posts\/151273\/revisions\/217360"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fivemx.com\/it\/wp-json\/wp\/v2\/media\/151274"}],"wp:attachment":[{"href":"https:\/\/fivemx.com\/it\/wp-json\/wp\/v2\/media?parent=151273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fivemx.com\/it\/wp-json\/wp\/v2\/categories?post=151273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fivemx.com\/it\/wp-json\/wp\/v2\/tags?post=151273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}