beginner
25 min

Tables & Arrays

Understand Lua tables, the most powerful data structure in Lua.

ArraysDictionariesTable Methods

1
Introduction to Tables

Tables are the only data structure in Lua, but they can be used as arrays, dictionaries, or objects!

Array-style Tables

-- Array (indexed by numbers)
local fruits = {"Apple", "Banana", "Cherry"}

print(fruits[1])  -- Apple (Lua starts at 1!)
print(fruits[2])  -- Banana
print(fruits[3])  -- Cherry

Arrays in Lua start at index 1, not 0 like many other languages!

Dictionary-style Tables

-- Dictionary (key-value pairs)
local player = {
  name = "Steve",
  level = 10,
  health = 100,
  isAlive = true
}

print(player.name)    -- Steve
print(player["level"]) -- 10

Access values using dot notation or bracket notation.

2
Table Operations

Learn how to add, remove, and modify table elements.

Adding Elements

local inventory = {"Sword", "Shield"}

-- Add to end
table.insert(inventory, "Potion")

-- Add at specific index
table.insert(inventory, 2, "Helmet")

print(inventory[1])  -- Sword
print(inventory[2])  -- Helmet
print(inventory[3])  -- Shield
print(inventory[4])  -- Potion

table.insert() adds elements to arrays.

Removing Elements

local items = {"A", "B", "C", "D"}

-- Remove last element
table.remove(items)

-- Remove at index
table.remove(items, 1)

-- Result: {"B", "C"}

table.remove() removes elements from arrays.

Table Length

local numbers = {10, 20, 30, 40}

print(#numbers)  -- 4

for i = 1, #numbers do
  print(numbers[i])
end

Use # to get the length of an array-style table.

3
Iterating Through Tables

Different ways to loop through table elements.

ipairs for Arrays

local players = {"Alice", "Bob", "Charlie"}

for index, name in ipairs(players) do
  print(index .. ": " .. name)
end

ipairs() iterates through arrays in order.

pairs for Dictionaries

local stats = {
  strength = 10,
  intelligence = 15,
  dexterity = 8
}

for key, value in pairs(stats) do
  print(key .. ": " .. value)
end

pairs() iterates through all key-value pairs (order not guaranteed).

💡 Tips:

  • • Use ipairs() for arrays (numbered indices)
  • • Use pairs() for dictionaries or mixed tables
  • • Tables are passed by reference, not by value

Practice Exercises

Exercise 1: Inventory System

Create an inventory table and add/remove items.

Starter Code:

local inventory = {}

-- Add 3 items to inventory


-- Print all items
for i, item in ipairs(inventory) do
  print(i .. ": " .. item)
end
Show Solution
local inventory = {}

table.insert(inventory, "Sword")
table.insert(inventory, "Potion")
table.insert(inventory, "Shield")

for i, item in ipairs(inventory) do
  print(i .. ": " .. item)
end

Exercise 2: Player Stats Dictionary

Create a player stats table and print each stat.

Starter Code:

-- Create player table with name, level, health, and mana


-- Print all stats
for stat, value in pairs(player) do
  print(stat .. ": " .. value)
end
Show Solution
local player = {
  name = "Hero",
  level = 15,
  health = 100,
  mana = 50
}

for stat, value in pairs(player) do
  print(stat .. ": " .. value)
end

Ready for more?

Continue your learning journey

Executors.Online - Your Roblox Developer Hub