Creating and Using Arrays

10 min

Creating and Using Arrays

Picture of a robot and 3 lines of dialogue

Coding Arrays

Tables are a way to group multiple values together. Unlike variables that store just a single value, tables don’t have a fixed size and can hold a mix of different value types. With 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.

Example Array

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 a Talking NPC

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 lacks 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. Or, drag the file into Studio.

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 print(dialogueArray) won’t print the array. It’ll 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 called an index. In Lua, Indexes start at one, and go up by one for every value added.

In this 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, replace the current value of the variable dialogue with the second index by typing = dialogueArray[2].
Avoiding Index Errors

If the index doesn’t exist, you’ll receive a nil error.

For example, the above array only has 3 values, 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 Using 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 be increased each time a function is called to get to the next value in the array.

Change Dialogue Lines

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, add a new variable named dialogueIndex. Set the variable to 1 to start at the beginning of the array.
  1. In speak(), replace the index number in dialogueArray[2] with the variable you just created.
  1. At the bottom of the function, add 1 to dialogueIndex. That way, the next time speak() is called, the next string in the array will be displayed.
  1. Playtest amd click the NPC to see each dialogue string from the array.

Existing Error in the Script

Notice there’s an error in the Output Window once the script reaches the end of the array. 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 compared to the size of the array to know when the end has been reached. The size of an array can be found by typing #, without spaces, before an array’s 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.

Restart the Dialogue

  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, move this line under an else statement.
  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 Nested Loops Next Looping Through Arrays