Yuari
Documentation
Docs/Core functions/math.clamp

math.clamp

Limits a number to an inclusive minimum and maximum.

Syntax

math.clamp(x: number, min: number, max: number) -> number

Parameters

ParameterTypeDescription
xnumberValue to limit.
minnumberLower bound.
maxnumberUpper bound.

Returns

TypeDescription
numberx kept inside the range.

Description

Equivalent to math.max(min, math.min(max, x)).

If min is greater than max the result collapses to max.

Example

print(math.clamp(150, 0, 100))  -- 100
print(math.clamp(-4, 0, 100))   -- 0
print(math.clamp(42, 0, 100))   -- 42

Notes

  • math.clamp(x, lo, lo) — Returns lo, which is useful for hard-locking a value.