UevKF8zjRV_WEtpJsXhr
We use cookies on this site to enhance your user experience

pairs and iPairs

15

Chef using pairs and iPairs to make soup on a table

Learning Objectives Students will be able to:
  • Use pairs to iterate through dictionaries
  • Use ipairs to iterate through arrays
  • Create a recipe game
Prerequisites Students should:

pairs and iPairs

pairs() and ipairs() are functions that can be used with a for loop to go through each element of an array or dictionary without needing to set starting or ending points. pairs() is used with dictionaries, and ipairs() is used with arrays. The “i” in ipairs() stands for “index.”

Dictionaries and pairs

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().

Arrays and ipairs

Remember, the “i” in ipairs() is for “index.” Use ipairs() and arrays in combination with a for loop to work with ordered values like leaderboards.

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.
  1. Beneath the menu dictionary, add an empty array which will be filled with the customer’s choices later.
  1. Use pairs() to check if each ingredient in the dictionary is marked true or false. If true, add the ingredient to soup.
  1. Repeat the order back to the customer. On your own, try to:
  • 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.

  1. Add an else statement which tells customers if no ingredients were selected.

Optional Challenges

Instead of using the output window, use the NPC from Creating and Using Arrays to create a waiter to take customer orders.


Allow players to select an ingredient by touching a physical part or GUI button. For reference, see the article on Creating GUI Buttons.

Disable buttons for individual players to let them know they can’t select ingredients more than once.


Create 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.


Previous Intro to Dictionaries Next Intro to Module Scripts

Tags:
  • lua
  • coding
  • pairs
  • ipairs
  • tables