Skip to content
Berke edited this page Jun 23, 2026 · 1 revision

Lua Scripting Math Functions

The Tmath table provides utility functions for common mathematical operations, vector calculations, interpolation, angle conversion, and random number generation.


Namespace

Tmath

Angle Conversion

DegToRad

local radians = Tmath.DegToRad(degrees)

Converts an angle from degrees to radians.

Parameters

Name Type Description
degrees number Angle in degrees

Returns

number

Angle in radians.

Example

local angle = Tmath.DegToRad(90)

RadToDeg

local degrees = Tmath.RadToDeg(radians)

Converts an angle from radians to degrees.

Parameters

Name Type Description
radians number Angle in radians

Returns

number

Angle in degrees.

Example

local angle = Tmath.RadToDeg(3.14159)

Utility Functions

Clamp

local value = Tmath.Clamp(value, minValue, maxValue)

Restricts a value to a specified range.

Parameters

Name Type
value number
minValue number
maxValue number

Returns

number

The clamped value.

Example

local health = Tmath.Clamp(playerHealth, 0, 100)

Lerp

local value = Tmath.Lerp(a, b, t)

Linearly interpolates between two values.

Parameters

Name Type Description
a number Start value
b number End value
t number Interpolation factor

Returns

number

Interpolated value.

Example

local alpha = Tmath.Lerp(0, 100, 0.5)
-- Returns 50

Vector2 Functions

Vector2Distance

local distance = Tmath.Vector2Distance(a, b)

Returns the distance between two Vector2 values.

Parameters

Name Type
a Vector2
b Vector2

Returns

number

Distance between the vectors.


Vector2DistanceSquared

local distance = Tmath.Vector2DistanceSquared(a, b)

Returns the squared distance between two vectors.

This is faster than Vector2Distance() because it avoids a square root operation.

Parameters

Name Type
a Vector2
b Vector2

Returns

number

Squared distance.


Vector2Dot

local dot = Tmath.Vector2Dot(a, b)

Returns the dot product of two vectors.

Parameters

Name Type
a Vector2
b Vector2

Returns

number

Dot product result.


Vector3 Functions

Vector3Distance

local distance = Tmath.Vector3Distance(a, b)

Returns the distance between two Vector3 values.

Parameters

Name Type
a Vector3
b Vector3

Returns

number

Distance between the vectors.


Vector3DistanceSquared

local distance = Tmath.Vector3DistanceSquared(a, b)

Returns the squared distance between two vectors.

This is faster than Vector3Distance() because it avoids a square root operation.

Parameters

Name Type
a Vector3
b Vector3

Returns

number

Squared distance.


Vector3Dot

local dot = Tmath.Vector3Dot(a, b)

Returns the dot product of two vectors.

Parameters

Name Type
a Vector3
b Vector3

Returns

number

Dot product result.


Vector3Cross

local cross = Tmath.Vector3Cross(a, b)

Calculates the cross product of two vectors.

Parameters

Name Type
a Vector3
b Vector3

Returns

Vector3

The resulting perpendicular vector.

Example

local right = Tmath.Vector3Cross(forward, up)

Random Numbers

Random

Generates random integer values.

Overloads

Maximum Value
local value = Tmath.Random(max)

Returns a random integer in the range:

0 to max - 1
Parameters
Name Type
max number
Returns
number

A random integer between 0 and max - 1.


Minimum and Maximum
local value = Tmath.Random(min, max)

Returns a random integer in the range:

min to max
Parameters
Name Type
min number
max number
Returns
number

A random integer between min and max (inclusive).

Examples

local dice = Tmath.Random(1, 6)

local enemyIndex = Tmath.Random(10)

RandomF

Generates random floating-point values.

Overloads

No Parameters
local value = Tmath.RandomF()

Returns a random float in the range:

0.0 to 1.0
Returns
number

A random floating-point value between 0.0 and 1.0.


Maximum Value
local value = Tmath.RandomF(max)

Returns a random float in the range:

0.0 to max
Parameters
Name Type
max number
Returns
number

A random floating-point value between 0.0 and max.


Minimum and Maximum
local value = Tmath.RandomF(min, max)

Returns a random float in the range:

min to max
Parameters
Name Type
min number
max number
Returns
number

A random floating-point value between min and max.

Examples

local chance = Tmath.RandomF()

local speed = Tmath.RandomF(0.5, 2.0)

local spread = Tmath.RandomF(5.0)

Clone this wiki locally