Mixed-Shape Tables
Part of the Introduction To Luau section of Coddy's Lua journey — lesson 18 of 73.
Arrays hold many values of one type; maps hold one key type and one value type. But your most common Lua tables mix types under named fields — a player with a string name, a number score, a boolean flag. Luau types these with a shape: a table type that lists each field and its own type.
type Point = {x: number, y: number}
local origin: Point = {x = 0, y = 0}
print(origin.x)The type keyword creates a type alias — a reusable name for the shape. It exists only for the checker: no runtime value named Point is created. Fields can each have a different type:
type Player = {name: string, score: number, active: boolean}
local player: Player = {name = "Rio", score = 1500, active = true}
print(`{player.name} has {player.score} points`)With the shape declared, the checker knows player.score is a number and flags mistakes early: a typo like player.scroe (no such field), or assigning player.score = "high" (wrong type). This is the first taste — a later chapter digs much deeper into shapes, optional fields and methods.
Challenge
EasyDefine a type alias Player for a shape with three fields: name: string, score: number and active: boolean.
Create local player: Player with the name "Rio", score 1500 and active set to true.
Print, each on its own line:
- the player's name
- the player's score
- the player's active flag
- the exact line
Rio has 1500 pointsbuilt with string interpolation from the fields
Try it yourself
-- Write code here
-- 1) type Player = {name: string, score: number, active: boolean}
-- 2) create player: Player = {name = "Rio", score = 1500, active = true}
-- 3) print name, score, active, then the interpolated summary line
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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 ValuesVariadic FunctionsDefining Function TypesRecap: Typed Functions2Core Types
Basic Types: num, str, boolThe '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 TablesMulti-dimensional Typed Arraystable.unpack and VarargsRecap: Arrays and Maps