Codectionary / Developer documentation / Lua

Lua select and argument counts

select counts varargs including nil or returns a suffix starting at a requested position. It operates on argument lists rather than table lengths.

Syntax

select("#", ...); select(index, ...)

Examples

Lua select and argument counts

Run with standalone Lua 5.4: lua example.lua. These scripts target Lua 5.4 rather than Roblox Luau.

local function inspect(...)
    print(select("#", ...))
    for i = 1, select("#", ...) do
        local value = select(i, ...)
        print(i, tostring(value))
    end
end
inspect("Ada", nil, false)
  1. Output starts with 3, then pairs 1/Ada, 2/nil, 3/false. Assigning select to one local keeps only the first value of its suffix.

Best practices

  • select(index, ...) can return multiple values as a final argument. Capture a single local when only one value is intended.

Runtime & versions

Lua interpreter or a host application. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Lua reference manuals

Specifications & further reading