Menu
Coddy logo textTech

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 icon

Challenge

Easy

Define 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:

  1. the player's name
  2. the player's score
  3. the player's active flag
  4. the exact line Rio has 1500 points built 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction To Luau