Skip to content

Util

Reference for the util API.

TIP

Yielding at the end of a thread or job is not necessary.

Functions

util.create_thread

Create a thread that'll be called each frame, returning a thread id.

lua
---@param callback function()
function util.create_thread(callback): integer

util.remove_thread

Delete a thread using the id returned from create_thread.

lua
---@param id integer
function util.remove_thread(id): nil

util.create_job

Create a job that'll be called once.

lua
---@param callback function()
function util.create_job(callback): nil

util.yield

Yield the current callback.

lua
---@param time_ms? integer
function util.yield(time_ms?): nil

util.get_tick_count

Tick count returned from GetTickCount().

lua
function util.get_tick_count(): integer

util.str_split

Tokenize a string and returns the tokens.

lua
---@param str string
---@param delimiter string
function util.str_split(str, delimiter): string[]

util.str_random

Returns a random string of length.

lua
---@param length integer
function util.str_random(length): string

util.str_bytes

Returns a hex string as a list of bytes (FFFF to { 255, 255 })

lua
---@param str string
function util.str_bytes(str): integer[]

util.number_bytes

Returns a 4 byte integer as bytes (little endian).

lua
---@param num integer
function util.number_bytes(num): integer[]

util.random_int

Returns a random integer between min and max.

lua
---@param min integer
---@param max integer
function util.random_int(min, max): integer

util.random_float

Returns a random float (number) between min and max.

lua
---@param min number
---@param max number
function util.random_float(min, max): number

Examples

lua
local natives = require('natives')

-- called once
util.create_job(function()
  local hash = joaat('player_two')
  if request.model(hash) then
    natives.set_player_model(players.me().id, hash)
  end
end)

-- called per frame
local id = util.create_thread(function()
  gui.rect(vec2(100.0, 100.0), vec2(100.0, 100.0))
    :filled()
    :color(color(255, 255, 255, 100))
    :draw()
end)

-- deletes the thread after 5s
util.create_job(function()
  util.yield(5000)
  util.remove_thread(id)
end)

local tokens = util.str_split('this is a nice string', ' ')
print(tokens[1]) -- 'this'