Skip to content

Optional Parameters

Not Standard In Lua

This type of parameter is not used in standard Lua documentation, and instead exists for Roblox's strongly-typed Luau, which requires certain values to be a very specific type defined by the developer.

Optional Parameters are represented by enclosing function parameters within square brackets [] and then displaying their default value within these brackets. If this parameter is not specified by you, it will use the value in brackets instead. They can also be applied to properties, in which they will simply be placed after the property.

It may help you to call these "Default Values" instead, as that is their purpose: to provide a default value.



Examples


(number , [number = 100])

Different Than Nullable Types

Be careful to not confuse optional parameters with nullable types! While similar, nullable types function differently than optional parameters.

In Lua, omitted parameters are treated as nil, so putting nil in place of an optional parameter will not throw an error.

Whichever you choose to do (explicitly write down nil vs. just leave it blank) is ultimately up to you. However, it is strongly advised that you leave an optional parameter blank as to not confuse it with a nullable type.

When you run MyFunction, it expects requiredNumber to be defined by you. You can also optionally define optionalNumber, but if you don't, the function will act like you input 100.


number OptionalProperty [ = 100 ]

If you do not set this property, its value will be 100.