Skip to content

Math Extensions

Side: Shared (Client + Server)

Extensions to Lua's built-in math library. Available globally after loading twinded_libs.

Functions

math.lerp(start, finish, factor)

Linear interpolation between two values.

ParameterTypeDescription
startnumberStart value
finishnumberEnd value
factornumberInterpolation factor (0.0 to 1.0)

Returns: number


math.round(value, decimal_places?)

Rounds a number to the specified decimal places.

ParameterTypeDefaultDescription
valuenumberValue to round
decimal_placesinteger?0Number of decimal places

Returns: number


math.clamp(value, lower, upper)

Clamps a value between a minimum and maximum.

ParameterTypeDescription
valuenumberValue to clamp
lowernumberMinimum bound
uppernumberMaximum bound

Returns: number


math.toHex(value, with_prefix?)

Converts an integer to a hexadecimal string.

ParameterTypeDefaultDescription
valueintegerValue to convert
with_prefixboolean?falseIf true, prepends 0x

Returns: string


math.fromHex(hex_str)

Converts a hexadecimal string to an integer.

ParameterTypeDescription
hex_strstringHex string (with or without 0x prefix)

Returns: integer


math.toSigned(value)

Converts an unsigned 32-bit integer to a signed integer.

ParameterTypeDescription
valueintegerUnsigned 32-bit value

Returns: number

Examples

lua
-- Linear interpolation
local mid = math.lerp(0, 100, 0.5)  -- 50

-- Rounding
math.round(3.14159)       -- 3
math.round(3.14159, 2)    -- 3.14

-- Clamping
math.clamp(150, 0, 100)   -- 100
math.clamp(-5, 0, 100)    -- 0
math.clamp(50, 0, 100)    -- 50

-- Hex conversion
math.toHex(255)            -- "ff"
math.toHex(255, true)      -- "0xff"
math.fromHex("ff")         -- 255
math.fromHex("0xff")       -- 255

-- Signed conversion (useful for native hashes)
local hash = math.toSigned(0xDEADBEEF)

Premium RedM Scripts — Multi-Framework