Math

Library
Updated a month ago · v1.3.7
i

Shared Module — extends the global math table

All functions are added directly to the Lua math global. Accessible via math.* or lib.math.*.

math.round

Rounds a number to the specified decimal places.

lua
math.round(3.14159, 2) -- 3.14
math.round(1567)       -- 1567
ParameterTypeRequiredDescription
valuenumber | stringyesValue to round
placesnumber | stringnoDecimal places (default: 0)

math.clamp

Clamps a value between lower and upper bounds. Auto-swaps boundaries if inverted.

lua
math.clamp(15, 0, 10) -- 10
ParameterTypeRequiredDescription
valnumberyesValue to clamp
lowernumberyesLower bound
uppernumberyesUpper bound

math.interp

Simple linear interpolation between two values.

lua
math.interp(0, 100, 0.5) -- 50.0
ParameterTypeRequiredDescription
startnumberyesStart value
finishnumberyesEnd value
factornumberyesInterpolation factor (0–1)

math.lerp

Returns an iterator that yields interpolated values each frame over a duration.

lua
for value, step in math.lerp(0.0, 100.0, 1000) do
    print(value, step) -- step goes 0 → 1
end
ParameterTypeRequiredDescription
startnumber | vector | tableyesStart value
finishnumber | vector | tableyesEnd value (same type as start)
durationnumberyesDuration in milliseconds

The final iteration yields (finish, 1), then nil.

math.groupdigits

Formats a number with digit grouping.

lua
math.groupdigits(1000000) -- "1,000,000"
math.groupdigits(1000000, '.') -- "1.000.000"
ParameterTypeRequiredDescription
numbernumber | stringyesNumber to format
separatorstringnoSeparator character (default: ,)

math.toscalars

Parses numeric scalars from a string.

lua
local x, y, z = math.toscalars("1.0, 2.5, 3.0")
ParameterTypeRequiredDescription
inputstringyesString containing numeric values
minnumbernoMinimum clamp value
maxnumbernoMaximum clamp value
roundboolean | numbernotrue to round all, or number to only round the first N values

math.tovector

Converts a string or table to a vector type.

lua
math.tovector("1.0, 2.5, 3.0") -- vector3(1.0, 2.5, 3.0)
math.tovector({x = 1, y = 2})  -- vector2(1, 2)
ParameterTypeRequiredDescription
inputstring | tableyesInput value — tables support {x,y,z} or {x=,y=,z=}
minnumbernoMinimum clamp
maxnumbernoMaximum clamp
roundboolean | numbernoRound flag

Returns vector2, vector3, vector4, or number depending on the number of components.

math.normaltorotation

Converts a surface normal vector to a rotation vector.

lua
local rot = math.normaltorotation(surfaceNormal) -- vector3(pitch, yaw, 0)

math.torgba

Converts a string or table to an RGBA vector4.

lua
local rgba = math.torgba("255, 128, 0, 0.5") -- vector4(255, 128, 0, 0.5)

RGB values are 0–255, alpha is 0–1.

math.hextorgb

Converts a hex colour string to RGB components.

lua
local r, g, b = math.hextorgb("#FF00AA")

math.tohex

Converts a number to a hex string.

lua
math.tohex(255)       -- "0xff"
math.tohex(255, true) -- "0xFF"
ParameterTypeRequiredDescription
nnumberyesNumber to convert
upperbooleannoUse uppercase hex letters
Was this page helpful?
This page was last updated on 12 March 2026
DIRKSCRIPTS
All you need for your FiveM server.10% off your first order

Unsubscribe any time.

Copyright © 2026 DirkScripts. Not affiliated with or endorsed by Rockstar North or Take-Two Interactive.