Menu
Coddy logo textTech

Creating a Table

Part of the Fundamentals section of Coddy's Lua journey — lesson 57 of 90.

In Lua, tables are created using curly braces {}.

The simplest table you can create is an empty one:

local myTable = {}

You can also create a table that already contains values by placing them inside the curly braces, separated by commas:

local numbers = {10, 25, 30}
local names = {"Alice", "Bob", "Charlie"}

When you create a table with values like this, Lua automatically assigns each value a numeric position starting from 1. So in the numbers table above, 10 is at position 1, 25 is at position 2, and 30 is at position 3.

challenge icon

Challenge

Easy

Create a game inventory system by building a table to store different weapon types. Declare a variable named weapons and assign it a table containing exactly three string values: "sword", "bow", and "staff". After creating the table, print the entire table to display your weapon collection (given).

Cheat sheet

Tables in Lua are created using curly braces {}.

Create an empty table:

local myTable = {}

Create a table with values (separated by commas):

local numbers = {10, 25, 30}
local names = {"Alice", "Bob", "Charlie"}

Lua automatically assigns numeric positions starting from 1 to values in tables.

Try it yourself

-- TODO: Write your code here

-- Print the weapons table - DONT MODIFY
print(table.concat(weapons, "\t"))
quiz iconTest yourself

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

All lessons in Fundamentals