Syntax
local bounded = math.clamp(value, minimum, maximum)Examples
Luau clamped numeric controls
Run as a standalone current Luau script, or as a Script in Roblox Studio ServerScriptService. No Roblox engine APIs are required; type-annotated syntax is not standard Lua.
local health, maximum = 140, 100
local bounded = math.clamp(health, 0, maximum)
local fraction = bounded / maximum
print(bounded)
print(fraction)- Output: 100, then 1. Over-range health produces a full rather than overflowing progress fraction.
Best practices
- Ensure minimum <= maximum and a positive denominator. Clamping alone does not validate NaN, infinity or client authority.
Runtime & versions
Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Luau documentation