AI Core

Lib for Lua

aiLib - the lib for Lua resources, loaded from ai-lib like ox_lib.

ai-lib carries a Lua port of the lib. It runs inside the resource that loads it, so points, keybinds, commands and callbacks belong to that resource and go with it.

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

Modules load on first use: aiLib.notify(...), aiLib.Points.new(), aiLib.addCommand(...). Calls that wait (triggerServerCallback, menu.show, progress, spawnVehicle, progressFor) must run in a thread. The types are the AiLib class in ai-lib's init.lua.

JavaScript resources without a bundler load @ai-lib/dist/client.js / server.js instead: the same aiLib global with the TypeScript lib's /shared plus /client or /server helpers.

Both sides

FunctionWhat
aiLib.side'client' or 'server'.
aiLib.every(ms, fn, options?)Periodic work on the resource's one scheduler thread (0 = every frame); job:set(active), job:stop(), job.running. A job never Waits.
aiLib.waitFor(check, timeout?, interval?)Waits until check returns a value.
aiLib.formatText(text, ...){0} placeholders.
aiLib.locale(resource?)Returns t(key, ...) and the merged lang table.
aiLib.fail(error){ ok = false, error = error } for server actions.
aiLib.numbers.round(value, decimals?), groupDigits(value, separator?), headingTo(from, to)Number helpers.
aiLib.stripGameText(text)HTML tags and GTA codes removed.
aiLib.notify(...)Client (text, kind?, duration?); server (source, text, kind?, duration?).
aiLib.inventory, aiLib.world, aiLib.appearanceThrough ai-core; the members differ per side (below).

Client

FunctionWhat
aiLib.cacheped, playerId, serverId, vehicle, seat, weapon; cache.onChange(key, handler).
aiLib.playerData.get(key), playerData.onChange(key, handler)ai-core's player data (character, money, jobs, duty, gangs, activeGang).
aiLib.triggerServerCallback(name, args?, timeout?)Asks the server; returns the answer and an error.
aiLib.onServerCallback(name, handler), aiLib.onServerEvent(name, handler)Answers the server, listens to server events.
aiLib.callAction(name, args?, { fallback, timeout?, silent? })Runs a server action, shows the translated refusal, returns the ok reply or nil.
aiLib.progress(options)ai-lib's progress bar (anim, scenario, canCancel, whileDead, freeze, disable); true when it ran out. progress.cancel(), progress.isRunning().
aiLib.menu.show(request, listeners?)A menu ({ title, items, back? }, listeners onHover, onToggle); returns the picked id.
aiLib.input(request)An input dialog ({ title, confirm, fields }); returns the values or nil.
aiLib.itemPicker({ title, target? })Admin tools: ai-core's item grid.
aiLib.skillCheck(difficulty, keys?)Skill check; true when every stage was hit.
aiLib.lockpick({ difficulty?, pins?, picks? })'opened', 'broken' or 'cancelled'.
aiLib.showPrompt(id, text), hidePrompt(id), onPromptPressed(handler)ai-lib's prompt.
aiLib.Points.new()Interaction points: :add(id, options), :addModel(id, options), :addVehicle(id, options), :remove(id), :clear(), :refresh().
aiLib.Zones.new()Zones: :add(id, options), :remove, :clear, :isInside.
aiLib.LocalPed.new(options)A local ped (attach, seat); :remove().
aiLib.Blips.new(), aiLib.Blips.create(options)Blips: :add, :get, :remove, :clear.
aiLib.Keybind.new(options)A rebindable key (hint, icon, hintLabel); :disable(state).
aiLib.playercoords, heading, distanceTo, freeze, teleport (ground = 'find' or no z: the ground under x / y), playAnim, stopAnim.
aiLib.playerPed(serverId)A player's ped, 0 out of scope.
aiLib.nearestPlayer(metres, { from?, filter? })The closest other player's server id.
aiLib.nearestVehicle(metres, { networked?, notOwn?, filter? })The closest vehicle.
aiLib.waitForEntity(netId, timeout?)The entity once it exists here, 0 after the timeout.
aiLib.waypoint()The map waypoint { x, y }.
aiLib.onShot(handler)The frame the local ped fires.
aiLib.fadeOut(ms?), aiLib.fadeIn(ms?)Screen fades.
aiLib.controls.disable(reason, controls, group?), disableAll(reason, except?), enable(reason)Disabled controls.
aiLib.hud.hide(reason), show(reason), visible()Hide HUDs.
aiLib.keyHints.show(id, { label, keybind?, key?, icon? }), hide(id)Key help lines.
aiLib.gestures.isHandsUp(), isPointing(), provider()Hands up and pointing.
aiLib.radial.add(item), remove(id), refresh()Radial items through ai-core.
aiLib.inventory.openInventory(), hasItem(item, count?), currentWeapon()The active inventory.
aiLib.world.pauseSync(reason), resumeSync(reason)Pause the weather and time sync.
aiLib.appearance.apply(ped, look), read(ped)Looks through the active clothing script.
aiLib.requestModel, requestAnimDict, requestAnimSet, requestPtfxAsset, requestTextureDict, requestWeaponAsset (name, timeout?)Load a game asset.
aiLib.pedHeadshot(ped, transparent?, timeout?)A headshot handle and texture name.
aiLib.locationName(coords?), vehicleLabel(vehicle), groundZ(x, y, z?)Names and ground height.
aiLib.raycast(from, to, options?)A line probe: { coords, normal, entity } or nil.
Lua
local points = aiLib.Points.new()
points:add('shop', {
  coords = vector3(25.7, -1347.3, 29.5),
  label = 'Open shop',
  icon = 'basket-shopping',
  onPressed = function()
    local pick = aiLib.menu.show({ title = 'Shop', items = { { id = 'water', label = 'Water' } } })
    if pick then aiLib.callAction('myshop:buy', { pick }, { fallback = 'BUY_FAILED' }) end
  end,
})

Server

FunctionWhat
aiLib.onClientCallback(name, handler, { rate?, lock?, ace? })Answers client callbacks through the gate.
aiLib.onClientEvent(name, handler, options?)A client net event through the gate.
aiLib.triggerClientCallback(name, source, args?, timeout?)Asks a client; returns the answer and an error.
aiLib.isAdmin(source, ace?)Whether the player is an ai-core admin (or has ace).
aiLib.RateLimit.new(count, windowMs)A per-player burst limit: limit:take(source), limit:release(source).
aiLib.progressFor(source, options)ai-lib's bar on the player's client, timed by the server (waits).
aiLib.minigameFor(source, options)A minigame on the player's client: 'won', 'lost' or 'broken' (waits).
aiLib.addCommand(name, { help?, params?, restricted? }, handler)A command; handler(source, args, raw).
aiLib.onCharacter({ loaded?, unloaded? })Character load and unload, replaying characters loaded before a restart.
aiLib.onlinecharacter, name(src, fallback), jobs, gangs, jobGrade, gangGrade, dutyJob, sources (asks ai-core per call).
aiLib.spawnVehicle(options)A server vehicle (model, coords, asker, plate, fuel, keysTo, purpose, driver, seats, props, setup, locked, bucket, persistent, bags); waits.
aiLib.playersNear(source, radius), playersNearEntity(entity, radius)Players around.
aiLib.nearestPlayerTo(source, metres, filter?), nearestVehicleTo(source, metres, filter?)The closest player or vehicle.
aiLib.vehicleFromNet(netId), vehicleNearPlayer(source, netId, { metres?, driver? })A live vehicle from a client's net id, or 0.
aiLib.aiLog(channel, message, options?)A log through ai-core.
aiLib.inventoryaddItem, removeItem, getItemCount, hasItem, canCarryItem, getItems, clearInventory, registerStash, openInventory, registerUsableItem, getItemDefinition.
aiLib.vehicleKeys.give(source, vehicle, purpose?), remove, hasVehicle keys through ai-core.
aiLib.phonenumber, sourceOf, hasPhone, mail, message, notify.
aiLib.dispatch.alert(alert)A police alert through ai-core.
aiLib.prison.jail(src, minutes, by?), release(src), timeLeft(src)Jail sentences.
aiLib.worldgetWeather, setWeather, getTime, setTime, setBlackout, freezeTime.
aiLib.appearance.get(charId), provider()A character's stored look (waits).

Not on the Lua side: the admin panel classes, PendingActions, PlayerMap, Cooldowns, the strict input readers, the state bag classes (Lua reads Entity(e).state itself) and the database (Lua resources use ai-mysql's oxmysql API).

On this page