AI Core

Lib: server

@aicore8/lib/server and the server side of aiLib: what they are, how to load them in TypeScript and Lua, and every topic page.

The server lib is the set of helpers every ai-* server script uses instead of writing the same code again: answering clients safely, commands, players, items, vehicles, money, logs and the admin panel. Most helpers that touch another system (inventory, keys, phone, prison...) go through ai-core, so your script keeps working when the server owner swaps the inventory or the phone script.

TypeScript

Import from @aicore8/lib/server in files under resource/server/. Helpers that work on any side (refusals, input readers, math) come from @aicore8/lib/shared.

resource/server/index.ts
import { onClientCallback, inventory, notify } from "@aicore8/lib/server";
import { fail, oneArg, readId } from "@aicore8/lib/shared";

Every resource made from the ai-core template already depends on the package ("@aicore8/lib": "workspace:*" in package.json); the build bundles it into dist/server.js.

Lua

Lua resources get the same helpers on the global aiLib table from the ai-lib resource. Add two lines to your fxmanifest.lua:

fxmanifest.lua
dependency 'ai-lib'
shared_script '@ai-lib/init.lua'

Then call aiLib.<name> in any server file. Each module loads the first time you use it and runs inside your resource, so commands and callbacks you register belong to your resource and go away when it stops.

server.lua
aiLib.addCommand('hello', { help = 'Says hello' }, function(source)
    aiLib.notify(source, 'Hello!', 'success')
end)

Lua functions marked Waits (progressFor, minigameFor, spawnVehicle, triggerClientCallback, billing.unpaid, appearance.get) pause the code until the answer comes. They only work inside a thread. Handlers of aiLib.onClientCallback and aiLib.onClientEvent already run in one; anywhere else, wrap the call in CreateThread(function() ... end).

Not every TypeScript helper has a Lua twin. Each entry says so when it has none. The ones missing in Lua: PendingActions, PlayerMap, Cooldowns, KeyedCooldowns, SourceLock, KeyWatchers, the position helpers, AreaStream, the state bag classes, AdminPanel, DataFile and the database helpers (Lua resources use ai-mysql's oxmysql API for SQL).

Topics

Helpers shared with the client

The server entry also re-exports the /fivem helpers, so you can import them from @aicore8/lib/server too. They are documented once on Lib: shared and fivem:

registerExport, resourceExport, optionalExport, callExport, provideExport, isResourceStarted, createLocale, loadLocaleTexts, LOCALE_CONVAR, ByResource, caller, onStop, plateOf, Reasons, every (with the types EveryOptions and Job), stateBoolean, stateNumber, stateString (type StateParse).

The state bag classes EntityState, PlayerState, GlobalKey and GlobalGroup exported from the server entry are the server versions: they add set. See State bags.

Two helpers only ai-core uses

sendPlayerData(source, key, value) and patchPlayerData(source, key, patch) push one value of a player's data (AiPlayerData) to that player's client. ai-core calls them; your script never needs them. Read player data on the client with getPlayerData (Lib: client).

On this page