6 min read
FiveM NUI From Scratch: Building an In-Game UI Panel With HTML, SendNUIMessage and NUI Callbacks
What This FiveM NUI Tutorial Builds (It’s Chrome in a Trench Coat)
Every slick interface you’ve seen in FiveM is secretly a web page. The banking app, the phone, the inventory grid, all of them are HTML rendered by an embedded browser. NUI is FiveM’s built-in Chromium layer, and this FiveM NUI tutorial builds a working in-game panel with it, end to end, starting from an empty folder.
The short version: you declare an HTML page in fxmanifest.lua with ui_page, push data from Lua to JavaScript with SendNUIMessage, and send data back through NUI callbacks using fetch and RegisterNUICallback. Add SetNuiFocus so the mouse works, and that’s the whole system.
We’ll build a small player panel that opens on command, shows data from Lua, and closes cleanly with a button or ESC. It’s deliberately boring, because boring is easy to extend. Once these five files make sense, every polished script UI you’ve bought, including the ones from xdopestore.com, stops being magic and starts being “oh, it’s just a div”.
Step 1: The Resource Skeleton and fxmanifest.lua
Create a resource folder with the front-end files tucked into an html subfolder. Five files total.
my-panel/
fxmanifest.lua
client.lua
html/
index.html
style.css
script.js
The manifest tells FiveM which page to load and which files the browser is allowed to touch.
fx_version 'cerulean'
game 'gta5'
client_script 'client.lua'
ui_page 'html/index.html'
files {
'html/index.html',
'html/style.css',
'html/script.js'
}
Two rules here. ui_page takes a path relative to the resource root, and every asset your page loads must appear in files. Miss the CSS entry and the panel renders like a 1998 GeoCities page, because the stylesheet quietly 404s inside the browser where you can’t see it. The full manifest reference lives on docs.fivem.net if you want the deep cut.
Step 2: HTML and CSS (Keep the Body Transparent)
The page itself is plain web dev. A container, a value we’ll fill from Lua, and a close button.
<link rel="stylesheet" href="style.css">
<div id="panel" class="hidden">
<h3>Player Panel</h3>
<p>Cash: <span id="money">$0</span></p>
<button id="close-btn">Close</button>
</div>
<script src="script.js"></script>
body {
margin: 0;
/* no background rule here, ever */
}
#panel {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 22vw;
padding: 2vh 1.5vw;
background: rgba(18, 18, 22, 0.92);
color: #eee;
border-radius: 8px;
font-family: sans-serif;
}
.hidden {
display: none;
}
The NUI page covers the entire screen, all the time. Give body a background colour and your players get a lovely solid rectangle instead of Los Santos. Keep the body transparent and style only your containers. Use vw and vh units instead of fixed pixels too, because your players run everything from a 1080p laptop to an ultrawide wider than their driveway.
Step 3: Lua to JavaScript With SendNUIMessage
Opening the panel is a one-way message from the game to the page. SendNUIMessage takes a Lua table, serialises it to JSON, and delivers it to the browser as a message event.
-- client.lua
local panelOpen = false
RegisterCommand('panel', function()
panelOpen = true
SetNuiFocus(true, true)
SendNUIMessage({
action = 'open',
money = 12500
})
end, false)
On the JavaScript side, one listener sorts messages by an action field. Nothing enforces that convention, but nearly every resource in the ecosystem uses it, so future you will thank present you.
// script.js
window.addEventListener('message', (event) => {
const data = event.data;
if (data.action === 'open') {
document.getElementById('money').textContent = '$' + data.money;
document.getElementById('panel').classList.remove('hidden');
}
if (data.action === 'close') {
document.getElementById('panel').classList.add('hidden');
}
});
Register that listener at the top level of your script, not inside some deferred init. A message that arrives before the listener exists simply evaporates, and you’ll spend an hour blaming Lua for a race condition you built in JavaScript.
Step 4: JavaScript Back to Lua With NUI Callbacks
Going the other way, the page talks to the game over HTTP. FiveM exposes each callback as a fake endpoint at https://<resource-name>/<callback>, and your JS calls it with fetch.
// script.js
function closePanel() {
document.getElementById('panel').classList.add('hidden');
fetch(`https://${GetParentResourceName()}/closePanel`, {
method: 'POST',
headers: { 'Content-Type': 'application/json; charset=UTF-8' },
body: JSON.stringify({})
});
}
document.getElementById('close-btn').addEventListener('click', closePanel);
Note GetParentResourceName(). FiveM injects it into the NUI context so the URL always matches whatever the resource folder is currently called. Hardcode my-panel there instead and the first person who renames the folder gets callbacks that 404 without any visible error. That person is usually you, three weeks later.
The Lua half registers a handler for the endpoint.
-- client.lua
RegisterNUICallback('closePanel', function(data, cb)
panelOpen = false
SetNuiFocus(false, false)
cb({})
end)
That cb({}) is not decoration. Every callback must answer, even with an empty object, or the fetch on the JS side sits there until it times out and fails. Everything crossing this bridge also has to be JSON-encodable, in both directions, so no functions and no userdata.
One more thing while we’re here. NUI input is client input, and clients lie. If your panel grows a “buy item” button, the callback should trigger a server event that validates price, stock and money on the server. Frameworks handle much of that plumbing for you; the QBCore scripts on qb-tebex.io lean on this exact callback pattern for nearly every menu and app they ship.
Step 5: Focus and the ESC Key (The Part Every Tutorial Skips)
[right, serious bit] SetNuiFocus(hasFocus, hasCursor) decides who owns the player’s keyboard and mouse. The first flag routes keyboard input to the page, the second shows the cursor. For a clickable panel you want both true on open and both false on close.
Here’s the trap. Set focus and never release it, and the player can’t move, can’t shoot and can’t open chat to complain. They’re stuck admiring your panel like a museum exhibit they’re not allowed to leave. Since keyboard input now goes to the browser, the game never even sees them mashing ESC, so you have to catch it yourself in JS.
// script.js
document.addEventListener('keyup', (e) => {
if (e.key === 'Escape') closePanel();
});
Because closePanel() already calls the callback that runs SetNuiFocus(false, false), ESC and the close button share one exit path. Wire this up before you build anything else, and test it first. If you do trap yourself during development, restart the resource from the F8 console or txAdmin and focus resets.
For HUD-style overlays that should never eat input, skip focus entirely. And if you need a UI that captures the mouse while the player keeps walking, look at SetNuiFocusKeepInput, which does exactly what the name promises.
Common Mistakes That Eat an Entire Evening
Five classics, listed here so you can lose your evening to something more original.
- Hardcoded resource name in fetch. Works until the folder gets renamed, then every callback dies quietly. Use
GetParentResourceName(). - Focus with no exit. ESC handling lives in JS, not Lua, because the game can’t see keys while NUI holds focus. Build the close path first.
- Assets missing from
files. The page loads but CSS or JS silently 404s. If the panel looks unstyled or buttons do nothing, check the manifest before the code. - Never calling
cb(). The fetch waits, times out and fails. Answer every callback, even with an empty object. - Opaque body background. The NUI page spans the whole screen, so a body background paints over the entire game. Transparent body, styled containers.
Where to Take the Panel From Here
You now have the full loop: the manifest declares the page, Lua opens it and feeds it data, JS renders and answers back, and focus arrives and leaves cleanly. Every inventory, phone and dealership UI in FiveM is this same loop wearing progressively nicer outfits.
Vanilla HTML and JS are genuinely fine at this size. When you graduate to a full inventory or an MDT with twelve views, that’s when React or Vue starts paying rent, and it’s also when studying finished work beats rereading docs. Pull apart how the premium FiveM scripts on scripts-tebex.io structure their interfaces and you’ll find the same SendNUIMessage and callback bones under every one of them.
The browser in the trench coat was the UI all along. Now go build something your players get stuck admiring on purpose, ideally with a working ESC key.