pairs and ipairs | Documentation - Roblox Creator Hub (2024)

Many scripts will often need to go through dictionaries or arrays. But sometimes, you may not know the start and end of these data structures. For example, a dictionary of players may be changing, or players may have an array inventory of varying sizes.

In these situations, you can use pairs() and ipairs(). These can be used with a for loop to go through each element without known start and end points.

This article will cover using them, and have a practical script example where you track restaurant orders in a cooking game.

Dictionaries and pairs()

pairs() is used with dictionaries. An example is seen below.

local myDictionary = {

  ["Blue Player"] = "Ana",

  ["Gold Player"] = "Binh",

  ["Red Player"] = "Cate",

}

for key, value in pairs(myDictionary) do

  print(key .. " is " .. value)

end

pairs() can be used to work with a dictionary element's key, value, or both. In the for loop below, the first variable is the key. The second variable is the value. The dictionary that you want to work with is passed in to pairs().

local inventory = {

["Gold Bricks"] = 43,

Carrots = 3,

Torches = 2,

}

print("You have:")

for itemName, itemValue in pairs(inventory) do

print(itemValue, itemName)

end

When executed, the code will print the following:

Arrays and ipairs()

ipairs() is used with arrays. The "i" in ipairs() stands for "index." Use ipairs() and arrays in combination with a for loop to work with ordered values like leaderboards. An example is below.

local players = {"Ali", "Ben", "Cammy"}

for playerRank, playerName in ipairs(players) do

print("Winner #" .. playerRank .. " is " .. playerName)

end

When executed, the code will print the following:

Winner #1 is Ali

Winner #2 is Ben

Winner #3 is Cammy

Practice - Restaurant Soup Simulator

Let's make some soup for a restaurant simulator where players pick from a list of possible soup ingredients. The ingredients will be stored as keys, and the values will start off as false so the players only get what they select.

Use pairs() to see what was picked, and then ipairs() to print the list of ingredients.

  1. Create a new dictionary named menu. Add 3 - 5 key-value pairs where the key is the ingredient and the value is false.

    local menu = {

    cabbage = false,

    beef = false,

    tomato = false,

    noodles = false,

    }

  2. Beneath the menu dictionary, add an empty array which will be filled with the customer's choices later.

    -- Customer's soup

    local isInSoup = {}

  3. Use pairs() to check if each ingredient in the dictionary is marked true or false. If true, add the ingredient to soup.

    -- Customer's soup

    local isInSoup = {}

    -- Adds customer's choices to their soup

    for menuChoice, value in pairs(menu) do

    if value then

    table.insert(isInSoup, menuChoice)

    end

    end

  4. Repeat the order back to the customer. In the script, code the following below.

    • Check if there is a menu item in isInSoup. If so, print "You ordered soup with: ".

    • Use ipairs() to go through the isInSoup array and print each ingredient.

    • Test by changing at least one menu item to true.

    -- Prints soup order from "isInSoup"

    if isInSoup then

    print("You ordered soup with: ")

    for index, soupIngredient in ipairs(isInSoup) do

    print(soupIngredient)

    end

    end

  5. In the if statement that checks if there is a menu item, add an else condition which tells customers if no ingredients were selected.

    if isInSoup then

    print("You ordered soup with: ")

    for index, soupIngredient in ipairs(isInSoup) do

    print(soupIngredient)

    end

    else

    print("Nothing was selected. Are you not hungry?")

    end

    The above step is an example of error checking in computer science. It's always a good practice to code what happens if there's a missing value possible in your code. If not, it's possible there might be an error during run time.

Optional Challenges

Below are some challenges that apply using pairs and ipairs in different ways. Try seeing if you can build out the code for these.

Challenge: Create a Waiter NPCInstead of using the output window, use the NPC from Intro To Arrays to create a waiter to take customer orders.

Challenge: Allow Players to Place OrdersAllow players to select an ingredient by touching a physical part such as a proximity prompt. For more information, see Proximity Prompts.

Advanced Challenge: Make Me Some Soup SimulatorCreate a restaurant simulator where players are the chefs! Players will have to put together ingredients to match the order given to them by the waiter.

  1. Create in-game props for 3 - 5 different ingredients.

  2. Create 3 - 5 recipes using dictionaries similar to the one in this lesson.

  3. Create an array that holds all of the recipes.

  4. Randomly select a recipe and use an NPC to tell players what the ingredients are.

  5. Create a new dictionary to make a recipe based off which prop ingredients the player touches.

  6. Compare the player's recipe to the recipe chosen by the NPC.

  7. Increase the challenge by adding an extra condition, such as a timer.

pairs and ipairs | Documentation - Roblox Creator Hub (2024)
Top Articles
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 5983

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.