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

Intro to Dictionaries

Hero with lua dictionary including health and money properties

Intro to Dictionaries

Dictionaries are tables that use a key to identify values instead of numbered indexes Values store data related to that key. Use dictionaries when you need to label values, not just list them in order as an array does.

Coding a Dictionary

Like arrays, dictionaries are created with curly brackets {} . Key value pairs are stored on separate lines followed by a comma. Keys and values can be any data type --including strings, numbers, and variable names. This dictionary uses strings as keys.

Formatting Keys

If the key is not a string and is a valid name with no spaces, the brackets aren’t needed. Below, a dictionary uses booleans to track if a player has collected orbs needed for a quest.

Character Dictionaries

Dictionaries are often used for organizing information for a character. Keys are used to label properties like name or health points. This examples uses a dictionary to do exactly that.

  1. Create a new Script.
  2. Type local hero = { and then press Enter to autocomplete the dictionary.
  1. Inside use name as a key.
  1. Name the hero by setting the key’s value and end the line with a comma.
  1. Add a second key value pair for health.
Don't mix dictionaries and arrays

Once you create a table, be consistent with using either key value pairs, or indexed values. Combining dictionaries and arrays can lead to errors.


Using Dictionary Values

To use individual dictionary values in code, type the name of the dictionary followed by the key in brackets. Example: dictionaryName[key]

  • Print the hero’s name and then playtest the code.

Use Unique Keys

Lua won’t stop you from trying to reuse the same key. Keep this in mind as you code. In the example below, the second value given for the key [“name”] will be printed.

Adding and Removing from Dictionaries

You’ll often need to add key-value pairs to an existing dictionary. For instance, adding players to a dictionary when they join the game, and then starting them off with 0 points.

Adding Key Value Pairs

  1. Type the name of the dictionary, followed by the name of the key in brackets.
      Example: playerPoints["name"]
  2. On the same line, set the value.
      Example : playerPoints["name"] = 0

Removing Key Value Pairs

  • Set the value to nil. This deletes the key.
      Example: playerPoints["name"] = nil

Variables as Dictionary Keys

Keys can also use variables. This example uses a variable to add a player’s name as a key when they join the game and then sets their points value to 0.

  1. In ServerScriptService, create a new script named PlayerPoints.
  2. Get the Players service and create an empty dictionary.
  1. Add a local function for setting player points with a parameter for a new player. Connect the function to the Players.PlayerAdded event.
  1. In the function, add a variable to get the player’s name and a print statement for testing.
  1. Insert name into the playerPoints dictionary as a key, and set the value to 0.
  1. Use name to print the name of the player and playerPoints[name] to print the value of the key matching the variable.

Here are other things you can try to do with dictionaries:

  • Create a trap part that does damage over time to a player. Once a player touches the trap, damage them, wait, then allow them to be damaged again.
  • Create a function that checks which of two players has the most points by accessing a dictionary.
  • Create a word dictionary with definitions for a made-up language or your favorite slang


Previous Making Changes to Arrays Next Intro to Module Scripts

Tags:
  • lua
  • coding
  • tutorial
  • tables