AI Core

Text and numbers

formatText, round, groupDigits, formatWeight, stripGameText, randomInt, pickRandom and rollChance, plus where ServerClock is documented.

Small helpers for what players read (money, weights, translated texts), random rolls, and countdowns that match the server's clock.

TypeScript
import { formatText, round, groupDigits, formatWeight, stripGameText, randomInt, pickRandom, rollChance, ServerClock } from "@aicore-fivem/lib/shared";

Text

formatText

Fills {0}, {1}... placeholders in a text with values by position. A translation may move or repeat a placeholder ("{1} paid {0}"), which is why the values go by number, not by order.

Side: Client, server and NUI · Waits: no

Signature
formatText(template: string, args: ReadonlyArray<string | number>): string
Lua
local text = aiLib.formatText(template, ...) -- values as separate arguments

Parameters

Prop

Type

Returns

string: the text with every placeholder that has a value filled in. A placeholder without a value stays as it is ({2}).

Example

import { formatText } from "@aicore-fivem/lib/shared";

formatText("You paid ${0} for {1} items", [250, 3]); // "You paid $250 for 3 items"

You rarely call it yourself: the locale helpers (createLocale, aiLib.locale, LangTexts) already fill placeholders. See Locale.

stripGameText

Makes text from another script plain: removes HTML tags, GTA colour and input codes (~r~, ~INPUT_PICKUP~) and &nbsp;, folds runs of spaces and trims. Use it before you show another script's text in your own UI.

Side: Client, server and NUI · Waits: no

Signature
stripGameText(text: unknown): string
Lua
local plain = aiLib.stripGameText(text)

Parameters

Prop

Type

Returns

string: the plain text. "" for null / undefined (Lua: nil).

Example

import { stripGameText } from "@aicore-fivem/lib/shared";

stripGameText("Press ~INPUT_CONTEXT~ to <b>open</b>  the ~g~shop~s~"); // "Press to open the shop"

Numbers

round

Rounds a number to a number of decimal places.

Side: Client, server and NUI · Waits: no

Signature
round(value: number, decimals?: number): number
Lua
local rounded = aiLib.numbers.round(value, decimals)

Parameters

Prop

Type

Returns

number: the rounded value.

Example

import { round } from "@aicore-fivem/lib/shared";

round(12.345, 1); // 12.3
round(12.5);      // 13

groupDigits

Writes a number with a separator every three digits: 1234567 becomes 1,234,567. Keeps the sign and the decimals. Use it for every amount of money players see.

Side: Client, server and NUI · Waits: no

Signature
groupDigits(value: number | string, separator?: string): string
Lua
local text = aiLib.numbers.groupDigits(value, separator)

Parameters

Prop

Type

Returns

string: the grouped number.

Example

import { groupDigits } from "@aicore-fivem/lib/shared";

groupDigits(1234567);        // "1,234,567"
groupDigits(-9876.5);        // "-9,876.5"
groupDigits(1500000, ".");   // "1.500.000"

formatWeight

Writes a weight in grams the way players read it: under 1 kg in grams, from 1 kg in kilograms with one decimal (whole kilograms from 100 kg), digits grouped.

Side: Client, server and NUI · Waits: no

Signature
formatWeight(grams: number): string

Lua: no twin.

Parameters

Prop

Type

Returns

string: "750 g", "1.5 kg", "1,250 kg".

Example

import { formatWeight } from "@aicore-fivem/lib/shared";

formatWeight(750);      // "750 g"
formatWeight(1500);     // "1.5 kg"
formatWeight(1250000);  // "1,250 kg"

Random

randomInt

A random whole number from min to max, both included.

Side: Client, server and NUI · Waits: no

Signature
randomInt(min: number, max: number): number

Lua: no twin; use math.random(min, max).

Parameters

Prop

Type

Returns

number: a whole number between min and max.

Example

import { randomInt } from "@aicore-fivem/lib/shared";

const payout = randomInt(150, 300); // 150, 151, ... or 300

pickRandom

One random item of a list.

Side: Client, server and NUI · Waits: no

Signature
pickRandom<T>(list: readonly T[]): T | undefined

Lua: no twin; use list[math.random(#list)].

Parameters

Prop

Type

Returns

One item, or undefined when the list is empty.

Example

import { pickRandom } from "@aicore-fivem/lib/shared";

const loot = pickRandom(["goldbar", "rolex", "diamond"]) ?? "goldbar";

rollChance

true with a chance of percent out of 100.

Side: Client, server and NUI · Waits: no

Signature
rollChance(percent: number): boolean

Lua: no twin; use math.random() * 100 < percent.

Parameters

Prop

Type

Returns

true about percent times out of 100.

Example

resource/server/index.ts
import { inventory } from "@aicore-fivem/lib/server";
import { rollChance } from "@aicore-fivem/lib/shared";

function useLockpick(source: number, slot: number): void {
  if (rollChance(25)) inventory.removeItem(source, "lockpick", 1, undefined, slot); // breaks 1 in 4 times
}

Roll on the server for anything that gives items or money. A roll on the client can be forced by a cheater.

Time

ServerClock

The server's clock on a client, for countdowns that do not depend on the client's own clock. It is exported from @aicore-fivem/lib/shared and has no Lua twin. Its full reference (sync, now, toLocal, countdown example) is on Camera and timing: ServerClock.

On this page