Logo

DIVERSITY

Documentation

Library

Math

Everything you need to know about the script.

Shared Functions


Round

client.lua
local roundedValue = dLib.Math.Round(value, places)
ArgumentTypeDescription
value
number | string
-
places?
number | string
-
Returned DataTypeDescription
roundedValue
number
-

Clamp

Restricts a value within a specified range.

client.lua
local clampedValue = dLib.Math.Clamp(value, min, max)
ArgumentTypeDescription
value
number | string
The value to clamp.
min
number | string
The minimum allowed value.
max
number | string
The maximum allowed value.
Returned DataTypeDescription
clampedValue
number
The clamped value.

ToHex

Converts a number to a hexadecimal string.

client.lua
local hexString = dLib.Math.ToHex(value)
ArgumentTypeDescription
value
number | string
The number to convert.
upper?
boolean
Whether to use uppercase letters.
Returned DataTypeDescription
hexString
string
The hexadecimal string representation.

HexToRGB

Converts a hexadecimal color string to RGB values.

client.lua
local r, g, b = dLib.Math.HexToRGB(hex)
ArgumentTypeDescription
hex
string
The hexadecimal color string (e.g., "#FF0000").
Returned DataTypeDescription
r
number
Red component (0-255).
g
number
Green component (0-255).
b
number
Blue component (0-255).

HexToRGBA

Converts a hexadecimal color string to RGBA values.

client.lua
local r, g, b, a = dLib.Math.HexToRGBA(hex)
ArgumentTypeDescription
hex
string
The hexadecimal color string.
Returned DataTypeDescription
r
number
Red component (0-255).
g
number
Green component (0-255).
b
number
Blue component (0-255).
a
number
Alpha component (0-255).

NormalToRotation

Converts a normal vector to a rotation.

client.lua
local rotation = dLib.Math.NormalToRotation(normal)
ArgumentTypeDescription
normal
vector3
The normal vector.
Returned DataTypeDescription
rotation
vector3
The resulting rotation.

ToVector

Tries to convert its argument to a vector3.

client.lua
local vec = dLib.Math.ToVector(input)
ArgumentTypeDescription
input
table
The table or object to convert to a vector.
min?
number
Optional minimum value to clamp components.
max?
number
Optional maximum value to clamp components.
round?
boolean
Whether to round component values.
Returned DataTypeDescription
vec
vector3
The resulting vector.

ToScalars

Converts a string into a set of scalar values.

client.lua
local x, y, z = dLib.Math.ToScalars(input)
ArgumentTypeDescription
input
string
The vector to decompose.
min?
number
Optional minimum value to clamp components.
max?
number
Optional maximum value to clamp components.
round?
boolean
Whether to round component values.
Returned DataTypeDescription
value
number
The set of scalar values.

ParseNumber

Parses a string or value into a number.

client.lua
local number = dLib.Math.ParseNumber(value, min, max, round)
ArgumentTypeDescription
value
string | number
The value to parse.
min?
number
Optional minimum value to clamp components.
max?
number
Optional maximum value to clamp components.
round?
boolean
Whether to round component values.
Returned DataTypeDescription
number
number
The parsed number.

Lerp

Linearly interpolates between two values based on a factor.

client.lua
local result = dLib.Math.Lerp(start, finish, duration)
ArgumentTypeDescription
start
number
The start value.
finish
number
The end value.
duration
number
The duration over which to interpolate over in milliseconds.
Returned DataTypeDescription
result
number
The interpolated value.

InverseLerp

Calculates the interpolation factor given a value between two points.

client.lua
local t = dLib.Math.InverseLerp(start, finish, value)
ArgumentTypeDescription
start
number
The start value.
finish
number
The end value.
value
number
The value between start and end.
Returned DataTypeDescription
t
number
The calculated factor (0.0 to 1.0).

Map

Maps a value from one range to another.

client.lua
local mappedValue = dLib.Math.Map(value, inMin, inMax, outMin, outMax)
ArgumentTypeDescription
value
number
The value to map.
inMin
number
Input range minimum.
inMax
number
Input range maximum.
outMin
number
Output range minimum.
outMax
number
Output range maximum.
Returned DataTypeDescription
mappedValue
number
The mapped value.

Deg2Rad

Converts degrees to radians.

client.lua
local radians = dLib.Math.Deg2Rad(degrees)
ArgumentTypeDescription
degrees
number
Angle in degrees.
Returned DataTypeDescription
radians
number
Angle in radians.

Rad2Deg

Converts radians to degrees.

client.lua
local degrees = dLib.Math.Rad2Deg(radians)
ArgumentTypeDescription
radians
number
Angle in radians.
Returned DataTypeDescription
degrees
number
Angle in degrees.

Sign

Returns the sign of a number (-1, 0, or 1).

client.lua
local sign = dLib.Math.Sign(value)
ArgumentTypeDescription
value
number
The number to check.
Returned DataTypeDescription
sign
number
The sign of the number.

AlmostEqual

Checks if two numbers are approximately equal.

client.lua
local isEqual = dLib.Math.AlmostEqual(a, b, epsilon)
ArgumentTypeDescription
a
number
First number.
b
number
Second number.
epsilon?
number
The tolerance (optional, default usually small).
Returned DataTypeDescription
isEqual
boolean
True if equal within tolerance.

Length2

Calculates the 2D length (magnitude) of a vector.

client.lua
local length = dLib.Math.Length2(x, y)
ArgumentTypeDescription
x
number
The x component.
y
number
The y component.
Returned DataTypeDescription
length
number
The 2D length.

Length3

Calculates the 3D length (magnitude) of a vector.

client.lua
local length = dLib.Math.Length3(x, y, z)
ArgumentTypeDescription
x
number
The x component.
y
number
The y component.
z
number
The z component.
Returned DataTypeDescription
length
number
The 3D length.

Distance2D

Calculates the distance between two 2D points.

client.lua
local distance = dLib.Math.Distance2D(x1, y1, x2, y2)
ArgumentTypeDescription
x1
number
X coordinate of the first point.
y1
number
Y coordinate of the first point.
x2
number
X coordinate of the second point.
y2
number
Y coordinate of the second point.
Returned DataTypeDescription
distance
number
The distance between points.

Distance3D

Calculates the distance between two 3D points.

client.lua
local distance = dLib.Math.Distance3D(x1, y1, z1, x2, y2, z2)
ArgumentTypeDescription
x1
number
X coordinate of the first point.
y1
number
Y coordinate of the first point.
z1
number
Z coordinate of the first point.
x2
number
X coordinate of the second point.
y2
number
Y coordinate of the second point.
z2
number
Z coordinate of the second point.
Returned DataTypeDescription
distance
number
The distance between points.

Was this page helpful?