math.clamp
Limits a number to an inclusive minimum and maximum.
Syntax
math.clamp(x: number, min: number, max: number) -> number
Parameters
| Parameter | Type | Description |
|---|---|---|
x | number | Value to limit. |
min | number | Lower bound. |
max | number | Upper bound. |
Returns
| Type | Description |
|---|---|
number | x 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)— Returnslo, which is useful for hard-locking a value.