Your First Luau Code
Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 3 of 80.
Time to write your first Luau code! The new ingredient on top of the Lua you know is the type annotation: a colon and a type name after the variable name.
local variableName: type = valueFor example, a variable that should only ever hold text:
local message: string = "Hello, World!"The : string part tells the type checker that message must contain string values. Try to assign a number to it later and the checker reports an error while you're still editing.
Luau also gives you a nicer way to build output: string interpolation. A string written with backticks can embed any expression inside curly braces:
local name: string = "Coddy"
print(`Hello, {name}!`) -- Hello, Coddy!
print(`2 + 3 is {2 + 3}`) -- 2 + 3 is 5Compare that with chaining .. everywhere: interpolation keeps the text readable while the {braces} do the work of concatenation.
Challenge
EasyStore the language name in a typed variable and greet it.
- Under the comment, declare
local language: string = "Luau". - Change the print to a backtick string:
print(`Hello, {language}!`).
Press Run Code: the output shows Hello, Luau!.
Try it yourself
-- Declare language here
print(``)
All lessons in Introduction To Luau
1Getting Started with Luau
What Is Luau?Why Use Luau?Your First Luau CodeType Checking & Error ModesRecap: Introduction to Luau4Working with Functions
Typing Params & Return ValuesTyping Anonymous FunctionsFunctions Returning NothingOptional ParametersDefault Parameter ValuesDefaults for BooleansVariadic FunctionsDefining Function TypesRecap: Typed Functions2Core Types
Number, String and BooleanThe 'any' Type: Escape HatchThe 'unknown' TypeNil & Optional TypesType Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Aliases, Unions, Intersections
Type Aliases for PrimitivesUnion TypesWorking with Union TypesLiteral TypesIntersection TypesCombining Type AliasesRecap: Advanced Type Combos3Typed Tables: Arrays & Maps
Typed ArraysAdding and Reading ElementsWhat is a Map Type?Declaring and Accessing MapsIterating TablesMixed-Shape TablesReading a GridBuilding a Gridtable.unpackRecap: Arrays and MapsPractice on your own: Online Luau compiler