lua-best-practices
Installation
SKILL.md
Lua best practices
Write resource Lua the way Overextended writes ox_inventory, ox_target and ox_fuel: one entry script per side, every other file a module loaded with require, data loaded with lib.load, settings read from convars, and ox_lib's cache and lib.* modules instead of hand-rolled loops. Checked against ox_lib 3.40.3. Read the ox resources line of .ox/config.md (walk up from the current directory) for the server's ox_lib version; if it is older, check anything below against that version's source.
Including @ox_lib/init.lua replaces the global require with ox_lib's own loader. It resolves paths from the resource root, not the calling file, and it can load files from other resources. Everything below assumes it.
Loading code and data
| Call | Use it for | How it behaves |
|---|---|---|
require 'client.utils' |
Code modules: files that define functions or state and return a table |
Runs the file once per resource and side, then returns the cached value to every later caller, so every module shares one instance. Errors on a circular require. A file that returns nothing gives true. |
lib.load 'data.stations' |
Data files (data/*.lua returning a table), and files run for their side effects |
Runs the file on every call and returns a fresh value; nothing is cached. Takes an optional environment table as its second argument. |
lib.loadJson 'data.prices' |
JSON data files | Reads data/prices.json and returns the decoded table. |
require '@ox_core.lib.init' |
Another resource's Lua library | Runs that file inside your resource: you get your own copy, not shared state. Read another resource's live state through its exports. |
Path rules, shared by all three: