FiveM Docs
es_extendedShared

Shared Functions

ESX Legacy functions available on both client and server - GetConfig, weapon lookups, ValidateType, IsValidLocaleString, GetRandomString and ESX.Await.

These functions are defined in es_extended/shared and exist on both the client and the server ESX object. They cover config access, weapon data from Config.Weapons, input validation and a few utilities. Math, table and timeout helpers have their own pages.

Overview

Resourcees_extended
SideShared (client and server)
Sourceshared/main.lua, shared/modules/*.lua, shared/compat.lua

Related shared pages: Math, Table, Timeout, Client PlayerData.

Getting the ESX object

ESX = exports['es_extended']:getSharedObject()

The getSharedObject export returns the ESX table. The @es_extended/imports.lua shared script does this for you and also keeps ESX.PlayerData in sync on the client.

Deprecated

The event esx:getSharedObject still answers for old scripts, but new code should use the export or the imports file. See Convert old ESX scripts.

Config

ESX.GetConfig

ESX.GetConfig(key)
NameTypeDescription
keystring?Config key. Without it the whole Config table is returned.

Returns: the value of Config[key], or the full config table.

if ESX.GetConfig('Multichar') then
    print('Multicharacter is enabled')
end

local inventory = ESX.GetConfig('CustomInventory') -- false or 'ox'

See Configuration for all keys.

Weapons

Weapon data comes from Config.Weapons (weapon config). Each entry has name, label, ammo (label, hash), tints and components (name, label, hash).

ESX.GetWeapon

ESX.GetWeapon(weaponName)
NameTypeDescription
weaponNamestringWeapon name, case-insensitive (weapon_pistol works).

Returns: index in Config.Weapons and the weapon table. Raises Invalid weapon name! for unknown weapons.

local _, weapon = ESX.GetWeapon('WEAPON_PISTOL')
print(weapon.label, weapon.ammo.label)

ESX.GetWeaponFromHash

ESX.GetWeaponFromHash(weaponHash)
NameTypeDescription
weaponHashnumber | stringWeapon hash, or a name that is hashed with joaat.

Returns: weapon table or nil. Does not raise errors.

local weapon = ESX.GetWeaponFromHash(GetSelectedPedWeapon(PlayerPedId()))

if weapon then
    print('Holding', weapon.label)
end

ESX.GetWeaponList

ESX.GetWeaponList(byHash)
NameTypeDescription
byHashboolean?When true, return a table keyed by weapon hash.

Returns: Config.Weapons (array) or the hash-keyed table.

ESX.GetWeaponLabel

ESX.GetWeaponLabel(weaponName)

Returns: string label (empty string if the entry has no label). Raises Invalid weapon name! for unknown weapons.

print(ESX.GetWeaponLabel('weapon_carbinerifle')) -- Carbine Rifle

ESX.GetWeaponComponent

ESX.GetWeaponComponent(weaponName, weaponComponent)
NameTypeDescription
weaponNamestringWeapon name.
weaponComponentstringComponent name from the config, for example suppressor.

Returns: component table (name, label, hash) or nil. Raises for unknown weapons.

local suppressor = ESX.GetWeaponComponent('WEAPON_PISTOL', 'suppressor')
if suppressor then
    GiveWeaponComponentToPed(PlayerPedId(), `WEAPON_PISTOL`, suppressor.hash)
end

Validation

ESX.ValidateType

ESX.ValidateType(value, ...)
NameTypeDescription
valueanyValue to check.
...stringAllowed Lua type names, for example 'string', 'number'.

Returns: true, or false and an error message such as bad value (string expected, got number). With no types given it returns true.

server/main.lua
RegisterNetEvent('shop:buy', function(item, amount)
    local ok, err = ESX.ValidateType(amount, 'number')
    if not ok then
        return print(('[shop] %s from %s'):format(err, source))
    end
end)

ESX.AssertType

ESX.AssertType(value, ...)

Same as ValidateType, but raises an error instead of returning false. Returns: true.

ESX.IsFunctionReference

ESX.IsFunctionReference(val)

Returns: true for functions and for tables with a callable __call metamethod (function references received from other resources).

local function runCallback(cb, ...)
    if ESX.IsFunctionReference(cb) then
        return cb(...)
    end
end

ESX.IsValidLocaleString

ESX.IsValidLocaleString(str, allowDigits)

Checks that a string only contains characters allowed for names, for example in character creation.

NameTypeDescription
strstringText to check. Must be valid UTF-8.
allowDigitsboolean?Also allow 0-9.

Returns: boolean

Always allowed: A-Z, a-z, space, - and Latin extended letters (accents). Extra ranges are added for Config.Locale values el (Greek), sr (Cyrillic), he (Hebrew), ar (Arabic) and zh-cn (Chinese), and for every charset enabled in Config.ValidCharacterSets.

print(ESX.IsValidLocaleString('Jean-Luc')) -- true
print(ESX.IsValidLocaleString('John_Doe')) -- false
print(ESX.IsValidLocaleString('Agent 47', true)) -- true

Utilities

ESX.GetRandomString

ESX.GetRandomString(length)
NameTypeDescription
lengthnumberNumber of characters. Fractions are rounded up, 0 or less returns ''.

Returns: string of random 0-9, A-Z and a-z characters. It uses math.random, so do not use it for secrets.

local reference = ESX.GetRandomString(8) -- e.g. "aZ3k9QpL"

ESX.Await

ESX.Await(cb, errMessage, timeout, interval)

Calls cb until it returns a non-nil value. Must run in a thread.

NameTypeDescription
cbfunctionCondition. Return any non-nil value to stop waiting. false counts as a result.
errMessagestring?Error text on timeout. Default failed to resolve callback.
timeoutnumber | false?Max wait in ms. Default 5000. false waits forever.
intervalinteger?Wait between checks in ms. Default 0.

Returns: the first non-nil value of cb. Raises an error on timeout.

local ped = ESX.Await(function()
    local p = PlayerPedId()
    return DoesEntityExist(p) and p or nil
end, 'Player ped did not load', 10000, 100)

Return nil, not false, while you are still waiting. ESX.Await stops on false because it only checks for nil.

ESX.DumpTable

ESX.DumpTable(table, nb)

Turns a table into a readable string for debugging.

NameTypeDescription
tableanyValue to dump. Non-tables are returned with tostring.
nbnumber?Starting indentation level. Default 0.

Returns: string

print(ESX.DumpTable(ESX.GetConfig('Accounts')))

ESX.Round

ESX.Round(value, numDecimalPlaces)

Shortcut for ESX.Math.Round.

Frequently asked questions

Independent documentation

esx-fivem.com is a community project and is not affiliated with the ESX Framework team. Facts on this page were checked against the upstream source. Official resources: docs.esx-framework.org and github.com/esx-framework.
Edit on GitHub

Last updated on

On this page