Utilities

The Utilities module provides helpful functions for common operations like waiting, printing, debugging, distance calculations, and more.

Compatibility

Shared - Works on both client and server-side

Available Methods

utils.print(...)

Enhanced print function with template support.

Parameters:

  • ... - Text and variables to print

Example:

-- Basic print
fivem.utils.print('Hello World')

-- Or use global access
utils.print('Hello World')

-- Print with template
fivem.utils.print('Player ${1} has ${2} health', playerName, health)

-- Or use global access
utils.print('Player ${1} has ${2} health', playerName, health)

-- Print with variable interpolation
local playerName = 'John'
local health = 100
utils.print('Player ${playerName} has ${health} health')

utils.dist(pos1, pos2)

Calculates the distance between two positions.

Parameters:

  • pos1 (table/vector3) - First position with x, y, z coordinates

  • pos2 (table/vector3) - Second position with x, y, z coordinates

Returns:

  • number - Distance between the two positions

Example:

utils.round(num, decimals)

Rounds a number to the specified number of decimal places.

Parameters:

  • num (number) - Number to round

  • decimals (number, optional) - Number of decimal places (default: 0)

Returns:

  • number - Rounded number

Example:

utils.server()

Checks if the code is running on the server-side.

Returns:

  • boolean - True if running on server, false otherwise

Example:

utils.client()

Checks if the code is running on the client-side.

Returns:

  • boolean - True if running on client, false otherwise

Example:

Common Utility Operations

Distance Calculations

Best Practices

  1. Use appropriate wait times - Don't use 0ms waits in loops

  2. Validate inputs - Always check parameters before using them

  3. Use debug logging - Enable debug mode for development

  4. Check environment when needed - Use utils.server() and utils.client()

  5. Format numbers appropriately - Use utils.round() for display

Migration from Traditional Methods

Traditional
Library Method

Wait(ms)

fivem.utils.wait(ms) or utils.wait(ms)

print(...)

fivem.utils.print(...) or utils.print(...)

math.sqrt(dx*dx + dy*dy + dz*dz)

fivem.utils.dist(pos1, pos2) or utils.dist(pos1, pos2)

math.floor(num + 0.5)

fivem.utils.round(num) or utils.round(num)

string.format(template, ...)

fivem.utils.tmpl(template, ...) or utils.tmpl(template, ...)

IsDuplicityVersion()

fivem.utils.server() or utils.server()

not IsDuplicityVersion()

fivem.utils.client() or utils.client()

Last updated