We use cookies on this site to enhance your user experience

Creating and Using Arrays

10 min

Creating and Using Arrays

Picture of a robot and 3 lines of dialogue

Tables are a way to group multiple values together. Compared to variables that store just a single number or string, tables don’t have a fixed size and can hold a mix of different value types. Using tables, you can group together stats for an in-game item, or create a list of thousands of player names.

There are different types of tables. One type is an array, which store lists of values in a specific order.

To create an array, name the array and use curly brackets { } . Separate values inside the brackets with commas like below:

local myArray = {“item1”, “item2”, 10, Workplace.Part, myVariable}

Creating and Using Arrays

To explore arrays, you’ll work with a non-playable character (NPC), that when clicked, will show a different line of dialogue stored in an array.

Set Up the Project

This project will use a premade NPC model which includes a script and Click Detector, but is lacking dialogue.

  1. Download the premade NPC.
  1. In the Explorer, import the NPC by right-clicking on Workspace > Insert From File > select Starter_NPC.rbxm.

Creating an Array for Dialogue

Create dialogue for the NPC to use by storing individual strings in an array. Each line will appear as a chat bubble when the NPC is clicked.

  1. In the Explorer, expand NPC and open DialogueScript.

  1. Between the variables and speak(), create an empty array to store dialogue by typing local dialogueArray = {}.
  1. Between the brackets {}, type at least three strings of dialogue, separated by commas.

Getting Contents of an Array

Now that an array is created, you’ll need a way to display the values in it.

Typing out print(dialogueArray) won’t print out the array. It’ll will just output the a series of numbers. Not very useful.

To get anything from an array, the script needs to know the location of that value in the array. Whenever a value is added to an array, it’s assigned a number. This number is called an index. Indexes start at one, and go up by one for every value added. For example, "Hi" is at index 1 and "Good bye!" is at index 3.

Value "Hi!" "Today's a great day!" "Good bye!"
Index 1 2 3

Keep in mind, not every coding language starts indexes at 1. Others, like Java for example, start at 0.

Getting Single Array Values

To use a value at a specific index, add the index after the array’s name without any spaces, like dialogueArray[1].

  1. To test out the array, set the variable dialogue to the second index by typing = dialogueArray[2].
Avoiding Index Errors

If you use an index that doesn’t exist, you’ll receive a nil error. For example, the above array only goes up to 3, so trying to get dialogueArray[4] will result in an error.

  1. Play the project and click the NPC to see the second array value appear in the chat bubble. Try changing the code to test out each value in the table.

Going Through Arrays with Variables

Variables can be used in place of the index number to make code more flexible. For instance, a variable can be used to get a random index number or increased each time a function is called to get to the next value in the array.

In this case, whenever a player clicks on the NPC, a variable named dialogueIndex, will increment by 1 to display the next line of dialogue.

  1. To keep track of the current index, create a new variable named dialogueIndex. To start at the beginning of the array, set the variable equal to 1.
  1. In speak(), replace the index number in dialogueArray[2] with the variable you just created.
  1. Towards the bottom of the function, add one to dialogueIndex. That way, the next time speak() is called, the NPC will display the next string in the array.
  1. Play the project. Click the NPC to see each dialogue string from the array.
Notice there's an error in the Output Window once the script reaches the end of the array, in this case on the 4th click. You'll fix this in the next section so the dialogue restarts from the beginning after it shows the last string.

Using Array Size for Repeating Code

One benefit of using variables in place of index numbers is that they can be used to check if you’ve reached the end of an array, and then set back to the first index value.

The size of an array can be found by typing #, without spaces, in front of that array’s variable name, for example #dialogueArray. Once you have how many elements are in the array, you can check it against the current value of the variable to know when it’s time to start back at the beginning.

  1. Code a new if statement that checks if dialogueIndex is equal to #dialogueArray. In the statement, restart dialogueIndex by setting it back to 1.
  1. If dialogueIndex isn’t at the end, it should still add 1 to dialogueIndex. To do this, add an else statement and under it, move the line:
dialogueIndex = dialogueIndex + 1
  1. Play the project. Check that you can cycle through the array without errors.

Issue: Dialogue doesn’t restart after reaching the end.

  • Check in the if statement that dialogueIndex is set back to 1. In the else statement, check that dialogueIndex has 1 added to itself.
  • When getting the size of the array, make sure there is no space after the # in #dialogueArray.

Challenge: Code the script so that the NPC’s dialogue goes backwards through the array. The dialogueIndex should start at the array and subtract each time instead of adding.

Challenge: Instead of showing dialogue in order, have the NPC show a random line of dialogue each time using Random.new.

Finished Project Sample

Project File

Download the finished project here.

Finished Script

These documents are licensed by Roblox Corporation under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Roblox, Powering Imagination, and Robux are trademarks of Roblox Corporation, registered in the United States and other countries.

Previous Creating a Point Giving Part Next Looping Through Arrays