Menu
Coddy logo textTech

Accessing Elements by Index

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

To access a specific element in a list-style table, you use square brackets with the position number of the item you want.

Here's the basic syntax:

local items = {"sword", "shield", "potion"}
print(items[1])  -- outputs: sword
print(items[2])  -- outputs: shield

There's something very important to remember about Lua: table indices start at 1, not 0. This is different from many other programming languages where the first element is at position 0. In Lua, the first element is always at index 1, the second at index 2, and so on.

local scores = {100, 85, 92}
print(scores[1])  -- first score: 100
print(scores[3])  -- third score: 92

This indexing system makes Lua tables feel more natural when thinking about positions - the first item is at position 1, just like you'd count in everyday life.

challenge icon

Challenge

Easy

Create a treasure chest inventory system that demonstrates table element access. First, create a table named treasureChest containing exactly four string values: "gold coin", "ruby gem", "magic scroll", and "silver key". After creating the table, use square bracket notation to access and print the third item from the treasure chest.

Cheat sheet

To access a specific element in a table, use square brackets with the position number:

local items = {"sword", "shield", "potion"}
print(items[1])  -- outputs: sword
print(items[2])  -- outputs: shield

Important: Lua table indices start at 1, not 0. The first element is at index 1, the second at index 2, and so on.

local scores = {100, 85, 92}
print(scores[1])  -- first score: 100
print(scores[3])  -- third score: 92

Try it yourself

-- TODO: Create the treasureChest table with the four items

-- TODO: Access and print the third item from the treasure chest
quiz iconTest yourself

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

All lessons in Fundamentals