Shared
Utilities

Utilities

RP.Util is a namespace of small helper functions available on both server and client.


RP.Util.RoundFloat(num, prec)

Rounds num to prec decimal places.

ParameterTypeDescription
numnumberValue to round
precintegerDecimal places

Returns number

RP.Util.RoundFloat(3.14159, 2) -- 3.14

RP.Util.Trim(value)

Strips leading and trailing whitespace from a string. Returns nil if value is nil or an empty string after trimming.

ParameterTypeDescription
valuestring?Input value

Returns string?

RP.Util.Trim("  hello  ")  -- "hello"
RP.Util.Trim("   ")        -- nil

RP.Util.RandomString(length)

Generates a cryptographically random alphanumeric string of the given length.

ParameterTypeDescription
lengthintegerDesired string length

Returns string

local token = RP.Util.RandomString(8) -- e.g. "aK7mPq3z"

RP.Util.GroupDigits(num)

Formats a number with thousands separators (commas).

ParameterTypeDescription
numnumberValue to format

Returns string

RP.Util.GroupDigits(1234567)  -- "1,234,567"
RP.Util.GroupDigits(9999)     -- "9,999"

RP.Util.GetDistance(v1, v2, useZ?)

Euclidean distance between two vector3 (or compatible table) values. Pass useZ = false to compute a flat 2-D distance.

ParameterTypeDescription
v1vector3First position
v2vector3Second position
useZboolean?Include Z axis (default true)

Returns number

local dist = RP.Util.GetDistance(coords1, coords2)
local dist2d = RP.Util.GetDistance(coords1, coords2, false)

RP.Util.Split(str, sep)

Splits str into an array of substrings using sep as a single-character delimiter.

ParameterTypeDescription
strstringString to split
sepstringSingle-character separator

Returns string[]

local parts = RP.Util.Split("steam:110000112345678:license:abc", ":")
-- { "steam", "110000112345678", "license", "abc" }