{"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":"introduccion-a-los-scripts-de-lua","status":"publish","type":"post","link":"https:\/\/rpcrate.com\/es\/introduction-lua-scripting\/","title":{"rendered":"Introducci\u00f3n a la programaci\u00f3n en Lua para FiveM"},"content":{"rendered":"<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=\"\" translate=\"no\">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<p>The configuration and code examples here target FiveM for GTA V Legacy. Enhanced has different runtime and compatibility rules; check <a href=\"https:\/\/docs.fivem.net\/docs\/developers\/legacy-vs-enhanced\/\">Cfx.re\u2019s Legacy-to-Enhanced changes<\/a> before applying them to an Enhanced server.<\/p>\n<div class=\"fivemx-guide-box fivemx-guide-note\">\n<p>The old <code data-no-translation=\"\" translate=\"no\">__resource.lua<\/code> manifest and the <code data-no-translation=\"\" translate=\"no\">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 id=\"create-a-minimal-resource\">Create a minimal resource<\/h2>\n<p>Inside your server-data <code data-no-translation=\"\" translate=\"no\">resources<\/code> directory, create <code data-no-translation=\"\" translate=\"no\">[local]\/hello_fivem<\/code>. The brackets group resources; the resource itself is the <code data-no-translation=\"\" translate=\"no\">hello_fivem<\/code> directory.<\/p>\n<p><code data-no-translation=\"\" translate=\"no\">fxmanifest.lua<\/code>:<\/p>\n<pre data-no-translation=\"\" translate=\"no\"><code data-no-translation=\"\" class=\"language-lua\" translate=\"no\">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=\"\" translate=\"no\">client.lua<\/code>:<\/p>\n<pre data-no-translation=\"\" translate=\"no\"><code data-no-translation=\"\" class=\"language-lua\" translate=\"no\">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=\"\" translate=\"no\">server.lua<\/code>:<\/p>\n<pre data-no-translation=\"\" translate=\"no\"><code data-no-translation=\"\" class=\"language-lua\" translate=\"no\">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\">resource manifest reference<\/a> and <a href=\"https:\/\/docs.fivem.net\/docs\/scripting-manual\/runtimes\/lua\/\" rel=\"nofollow noopener\">CfxLua guide<\/a>.<\/p>\n<h2 id=\"understand-client-and-server-contexts\">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=\"\" translate=\"no\">PlayerPedId<\/code> and <code data-no-translation=\"\" translate=\"no\">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=\"\" translate=\"no\">AddEventHandler<\/code> for events that should stay in one context. Use <code data-no-translation=\"\" translate=\"no\">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\">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 id=\"avoid-blocking-the-scheduler\">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=\"\" translate=\"no\">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 id=\"use-exports-for-a-clear-resource-boundary\">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=\"\" translate=\"no\"><code data-no-translation=\"\" class=\"language-lua\" translate=\"no\">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=\"\" translate=\"no\">exports.hello_fivem:formatPlayerLabel(id, name)<\/code>. Do not use an export to bypass permission or ownership checks.<\/p>\n<h2 id=\"run-and-test-the-resource\">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=\"\" translate=\"no\">refresh<\/code> and then <code data-no-translation=\"\" translate=\"no\">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=\"\" translate=\"no\">mycoords<\/code>.<\/li>\n<li>Add ensure hello_fivem to server.cfg after any dependencies, then restart the server to confirm normal startup.<\/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\">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\">first Lua script tutorial<\/a> before adding framework or database dependencies.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Los recursos de FiveM pueden usar CfxLua, un entorno de ejecuci\u00f3n modificado de Lua 5.4, para scripts de cliente y servidor. Un recurso actual comienza con fxmanifest.lua, enumera los archivos que carga cada contexto y mantiene las decisiones de autoridad, como permisos, dinero e inventario, en el servidor.<\/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:\/\/rpcrate.com\/es\/wp-json\/wp\/v2\/posts\/151273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rpcrate.com\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rpcrate.com\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rpcrate.com\/es\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rpcrate.com\/es\/wp-json\/wp\/v2\/comments?post=151273"}],"version-history":[{"count":3,"href":"https:\/\/rpcrate.com\/es\/wp-json\/wp\/v2\/posts\/151273\/revisions"}],"predecessor-version":[{"id":217360,"href":"https:\/\/rpcrate.com\/es\/wp-json\/wp\/v2\/posts\/151273\/revisions\/217360"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rpcrate.com\/es\/wp-json\/wp\/v2\/media\/151274"}],"wp:attachment":[{"href":"https:\/\/rpcrate.com\/es\/wp-json\/wp\/v2\/media?parent=151273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rpcrate.com\/es\/wp-json\/wp\/v2\/categories?post=151273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rpcrate.com\/es\/wp-json\/wp\/v2\/tags?post=151273"}],"curies":[{"name":"gracias","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}