EN · $ USD
FiveM Scripts and Development

How to Write FiveM Scripts With AI

7 min read Updated: OyunBulut technical team

In short

To write FiveM scripts with AI, give the AI clear context covering your framework, inventory and targeting scripts, folder structure and rules, and ask for the work in small pieces rather than all at once. Run the generated Lua code on a test server, measure it with resmon, and check that every server event validates the source, amount and distance.

In this guide
  1. Key concepts
  2. Step 1: Give the AI your framework context
  3. Step 2: Break the job into small pieces
  4. Step 3: An example of a good prompt
  5. Step 4: Review the output for security
  6. Step 5: Testing and performance measurement
  7. The debugging loop: How to report errors to the AI
  8. What to watch on the client side
  9. Common FiveM mistakes AI makes
  10. Which AI tool should you use?
  11. Next steps

Writing FiveM scripts with AI means describing the Lua code you want to an AI coding assistant, having it generate the code, then testing and hardening that code. With the right context, AI can scaffold a simple job, menu or Discord logging system in minutes. Without enough context, though, it invents functions that do not exist, and if you do not ask for security checks, it writes server events that are wide open to cheaters. This guide walks through a workflow that turns the output into a working, secure script.

Key concepts

  • Prompt: The written instruction you give the AI. It has the biggest impact on code quality.
  • Context: Everything the AI sees when it answers: your prompt, the files you attach and earlier messages.
  • Hallucination: When the AI uses a function or export that does not actually exist as if it did. It is the most common error in FiveM, because function names change between framework versions.
  • Server event: An event sent from the client (the player) to the server. It is the spot cheaters target most.

Step 1: Give the AI your framework context

Saying "write me a fisherman job" forces the AI to guess which framework, inventory and targeting system to write for. Start every session with a context block like this:

CONTEXT
- FiveM server, Lua 5.4 (fxmanifest: lua54 'yes')
- Framework: QBCore (exports['qb-core']:GetCoreObject())
- Inventory: qb-inventory
- Targeting: qb-target
- Database: oxmysql (use MySQL.query.await)
- Notifications: QBCore.Functions.Notify
- Language: All player-facing text in Turkish, in a locale file

RULES
- Do not invent exports or functions that do not exist; if unsure, ask.
- Money, item and job checks happen only in server.lua.
- Configurable values go in config.lua.
- Do not use unnecessary Wait(0) in loops.

If you have the relevant framework files (for example, the file that defines the player object's functions), add them to the context too. The AI's hallucination rate drops noticeably.

Step 2: Break the job into small pieces

Asking for a "complete gang system" in a single prompt produces long, inconsistent output that is hard to test. Instead, ask for it in order:

  1. fxmanifest.lua and the folder structure
  2. config.lua: coordinates, prices, durations
  3. client.lua: targeting, animations, progress bar
  4. server.lua: validation, item and money transactions
  5. Locale file and install notes

Test each piece as you get it, then move on to the next. When an error appears, knowing which piece it is in saves time. If you are not familiar with Lua and the FiveM file structure at all, read the guide to writing your first FiveM script in Lua first; knowing the basic syntax is enough to read the AI's output.

Step 3: An example of a good prompt

Using the CONTEXT above, write the server.lua file for a resource
named ob_balikci.

- Add a net event named 'ob_balikci:sat' with one parameter: miktar (amount).
- Reject the action if the player is more than 5 meters from Config.SatisNoktasi.
- Reject it if miktar is not an integer between 1 and 50.
- Reject it if the player does not have enough 'balik' in their inventory.
- The same player cannot sell more than once every 5 seconds.
- Payment: miktar * Config.BalikFiyati, in cash.
- At the end, explain in 3-4 bullet points which checks are done and why.

Notice that the prompt describes "which rules must hold," not just "what to do." If you do not ask for security checks explicitly, the AI often takes the shortest path, which means code with no validation.

Step 4: Review the output for security

In FiveM, client-side code runs on the player's computer and can be modified. So the golden rule is: never trust any value that comes from the client. The server-side code you should expect from the prompt above looks something like this:

local QBCore = exports['qb-core']:GetCoreObject()
local sonSatis = {}

RegisterNetEvent('ob_balikci:sat', function(miktar)
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    if not Player then return end

    miktar = tonumber(miktar)
    if not miktar or miktar ~= math.floor(miktar) or miktar < 1 or miktar > 50 then return end

    local simdi = os.time()
    if sonSatis[src] and simdi - sonSatis[src] < 5 then return end
    sonSatis[src] = simdi

    local coords = GetEntityCoords(GetPlayerPed(src))
    if #(coords - Config.SatisNoktasi) > 5.0 then return end

    local item = Player.Functions.GetItemByName('balik')
    if not item or item.amount < miktar then return end

    if Player.Functions.RemoveItem('balik', miktar) then
        Player.Functions.AddMoney('cash', miktar * Config.BalikFiyati, 'balik-satisi')
    end
end)

AddEventHandler('playerDropped', function()
    sonSatis[source] = nil
end)

Apply this checklist to every server event the AI generates:

  • Source: Is the player ID taken from the source variable rather than from a parameter?
  • Type and range: Are numeric parameters converted with tonumber and checked against a lower and upper bound?
  • Location: If the action must happen at a specific spot, is the distance measured on the server?
  • Ownership: Does the player actually have the item or money being removed?
  • Rate limit: What happens if the event fires hundreds of times per second?
  • Price: Does the payment amount come from the client? It should not; it should be calculated from the config.
  • SQL: Do queries use parameters (?), or do they concatenate strings?

You can ask the AI these questions too: a prompt like "Review this code through a cheater's eyes and list the points that could be exploited" often catches holes in the first output. Even so, the final check is your responsibility.

Step 5: Testing and performance measurement

  1. Copy the script to a separate test server, not your live server.
  2. Add the line ensure ob_balikci to server.cfg and start the server.
  3. Check the console for any SCRIPT ERROR lines. If there is an error, paste the line back to the AI exactly as it appears.
  4. In game, type resmon 1 in the F8 console to watch the script's CPU usage. An idle script should use a negligible amount of time.
  5. Deliberately try bad input: a negative amount, a distance that is too far, an item that is not in the inventory.

To interpret performance issues, see the FiveM resmon script optimization guide.

The debugging loop: How to report errors to the AI

Code from the AI often does not fully work on the first try. The way to keep the fix loop short is to give evidence instead of describing the error. An effective error report has four parts:

ERROR
[the SCRIPT ERROR line from the console, exactly as shown]

WHAT I DID
I clicked the NPC at the pier with ox_target and the menu did not open.

EXPECTED
The fishing rod rental menu should have opened.

RELEVANT FILE
[only the relevant part of client.lua]

With this format, the AI does not have to guess; the line number and error message point directly to most problems. If the same error is not fixed after two or three attempts, opening a new session and starting again with a clean context usually gets results faster than pushing on in a long conversation.

What to watch on the client side

Security checks happen on the server, but the client side matters for performance. In the client code the AI writes, check for loops that run every frame: if the player is far from a point, the loop should switch to a long wait. Whenever possible, ask for a targeting system (qb-target, ox_target) or zone-based triggers instead of constant distance checks. This one change noticeably cuts the CPU usage of an idle script.

Common FiveM mistakes AI makes

  • Mixing ESX and QBCore functions in the same file
  • Suggesting deprecated functions from older framework versions
  • Using a while true do Wait(0) loop where it is not needed
  • Handling money and item transactions on the client side
  • Leaving the file list in fxmanifest.lua incomplete

Which AI tool should you use?

General-purpose chatbots work for short code snippets, but when you work regularly across multiple files, the token quota runs out fast. If you write scripts often, a plan with a fixed token quota makes your spending predictable. OyunBulut's AI Developer Pro plan offers 1M tokens and usage tracking that shows your usage percentage; it is a good starting point if you write small scripts now and then. If you work regularly on multiple resources, AI Developer Pro + with 3M tokens leaves room for more sessions. The AI Developer plans guide explains tokens and the differences between plans in detail.

Next steps

  1. Adapt the context block above to your own server's framework and script list, and save it in a file.
  2. Pick a small script with a single event for your first try.
  3. Depending on how often you will use it, start with the AI Developer Pro or AI Developer Pro + plan, and apply the security checklist to every event.

Frequently asked questions

Are FiveM scripts written with AI secure?

Not by default. They become secure when you explicitly ask for security checks in your prompt and review every server event for source, range, distance and ownership.

How do I stop the AI from inventing functions?

Specify your framework, inventory and targeting scripts in the context, attach the relevant files if possible, and tell the AI to ask when it is unsure. Pasting console error lines back verbatim also speeds up fixes.

Can I write FiveM scripts with AI without knowing Lua?

It is possible for simple scripts, but learning basic Lua syntax is recommended so you can read the output and check it for security.

Can I upload an AI-written script straight to my live server?

That is not recommended. First test it on a separate test server for errors, performance and bad-input attempts.

Does the same method work for ESX?

Yes. Just specify ESX as the framework in the context block, along with the inventory and targeting scripts you use; the security checklist is the same.

For this guide

Recommended plans

AI Developer Pro and Pro + plans let you know your token quota up front and track your usage while you write FiveM scripts with AI.

AI Developer Pro

Recommended

1M tokens / month — usage percentage and stats tracking.

$9.83/mo

+VAT

  • LocationIstanbul
  • 1M tokens
  • % + stats
  • Automatic Setup

FiveM Services — tüm paketler

Need help with setup?

Send questions about your service in a support ticket, and our technical team will walk you through it step by step.