Codectionary / Developer documentation / Luau

Luau clamped numeric controls

math.clamp bounds a number between an inclusive minimum and maximum. It is useful for UI controls, progress values and gameplay parameters. It is a Luau library addition relative to standard Lua.

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)
  1. 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

Specifications & further reading