math.max

Shared

Returns the largest value among the given arguments


Syntax

local result = math.max(
    value,
    ...
)

Parameters

TypeNameDescription
numbervalueFirst number
number...Additional numbers to compare

Returns

TypeNameDescription
numberresultThe largest of all given values

Examples

Max of two numbers
local result = math.max(3, 7)

engine.print("info", result) -- 7
Max of multiple numbers
local result = math.max(1, 5, 3, 9, 2)

engine.print("info", result) -- 9
Max with negative numbers
local result = math.max(-10, -3, -7)

engine.print("info", result) -- -3
Max of a single value returns itself
local result = math.max(42)

engine.print("info", result) -- 42
Clamp a value to a minimum floor
local speed = -5
local clamped = math.max(speed, 0)

engine.print("info", clamped) -- 0
Find the highest score in a table
local scores = {42, 88, 17, 95, 63}
local best = scores[1]

for _, score in ipairs(scores) do
    best = math.max(best, score)
end

engine.print("info", best) -- 95
Ensure health does not drop below zero
local hp = -10
local safe_hp = math.max(hp, 0)

engine.print("info", safe_hp) -- 0

On this page