How to Use Arrays in Roblox – Roblox Scripting Tutorial

You can access the full course here: Explore Roblox Scripting with Lua

Intro to Arrays

In this lesson, we’re going to take a look at arrays inside Roblox.

To start off, create a new Roblox project using the Baseplate template:

Baseplate template

In the Explorer window, we see that our workspace basically only holds a baseplate and the spawn location currently:

baseplate spawn location

If we press F5 to enter the play mode, we’ll spawn in the spawn location and go from there:

play mode

What is an Array?

Unlike the usual variables that can hold one specific value of a certain type (such as numbers, strings, true or false values) or reference objects, arrays are variables that can be seen as a sort of container or a file cabinet. It holds multiple variables within it and we can access each and every one of them as needed.

Let’s create a script to see how it works in practice. To do that, hover your mouse over the ServerScriptService in the Explorer window, then click on the plus symbol that appears next to it and search for ScriptSelect it and go to the Properties window. Set the script Name to ‘ArrayTester‘:

Script properties

The way we instantiate a variable in Lua is by typing local to define the scope (in this case, only accessible by this script and not other scripts). We give it a name, then we add an equal sign and attribute it a value:

local number = 5

This would be an example of a normal variable that only stores one single value.

To create an array, we also add in the local keyword, a name, and the equal sign, but then we add the values in between curly braces like so:

local numbers = {5, 10, 15, 20}

Note that they need to be of the same data type, which means that if you have an array of whole numbers such as above then all values in the array must be whole numbers and not decimals or even values that aren’t numbers at all, for instance.

The Use of Arrays in Roblox

Arrays are very useful for us as we can have an array with the players in our game and thus perform commands or actions upon them all.

In terms of terminology, we have the element and the indexes that make up an array.

The elements are the values of the array (e.g. 5, 10, 15, and 20 are all values of the ‘numbers’ array above). On the other hand, the indexes are the numbers used to identify specific elements inside the array and stand basically for the position of that number in the array.

To print the third element of our numbers array, we do:

print(numbers[3])

In Lua, the indexes start at 1 so ‘numbers[1]’ will return the first element of the numbers array and so on.

You can test it out by clicking ‘Play‘ in the Script Menu tab or pressing F5.

Make sure you are displaying the Output window to be able to check the console. If it’s not visible, you can open it by clicking on ‘Output’ in the View tab:

Output view mode

To stop playing/testing, press Shift + F5.

Altering and Adding Elements to the Array

To alter an element, we access its index and make it equal to a new value.

Let’s change the first element of the array to be 1000:

local numbers = {5, 10, 15, 20}
numbers[1] = 1000
print(numbers[1])

Here, we shouldn’t see ‘5’ in the console. We should see ‘1000’ instead.

Now, to add an element such as a fifth element to our numbers array we access that index and attribute it some value:

local numbers = {5, 10, 15, 20}
numbers[5] = 25
print(numbers[5])

It works similarly with other types of data, like strings for example:

local strings = {Roblox, “Studio”, “Tutorial”}
print(strings[2])

Even parts, scripts, and other objects in Roblox can be put inside an array just like they can be assigned to a normal variable.

Transcript

Hey, everyone, and welcome. In this lesson, we are gonna begin by looking at arrays inside of Roblox and how it can improve our programming abilities.

So I’ve gone ahead and created a brand new Roblox project right here using the Baseplate template, which just includes a Baseplate right here and a SpawnLocation for us. All right. So if we press F5, it will spawn in on the SpawnLocation and we can pretty much play the game from here.

Okay. So let’s get started with creating some arrays. Now first of all, what is an array? Well, unlike a normal variable, which can hold a specific value, for example, we have variables that can hold numbers, that can hold strings, that can hold true or false values, variables that can reference parts.

Arrays are variables that act sort of like containers, or think of like a filing cabinet. It’s an array that can contain multiple variables within it that we can choose to access. So I think a good example would be to actually create a script and have a look at how it works.

So I’m gonna go over to our ServerScriptService right here, and I’m gonna create a brand new script. And let’s just call this one ArrayTester. Okay. So here inside of our script, what I’m gonna do is show you how we can create some arrays and modify them.

Now first of all, the way we create a variable inside of Lua here inside of Roblox Studio is we type local to define the scope. In our case, local means that this variable is only accessible by this script. And we then specify a name for the variable. Now let’s just say we wanna call this variable num, for number.

And then we can put an equal sign and give it a default value of, let’s just say, five. Now that is how we create a normal variable. We give it a local, we give it a name, and then we give it a default value. So how do we do this with array? Since I said arrays contain multiple different variables, how do we do that?

Well, what we’re gonna do down here is go local, but this time, we’re gonna go numbers. Let’s actually rename this one to number, actually, to make it a bit easier. So we’ve got number, which is five, and numbers, which is going to be an array of various different numbers, okay?

And this is going to equal, and what we need to do is put in two squiggly brackets, like so, and then we can just enter our numbers. So I can go 5, 10, 15, 20, and all the other numbers that you want, okay? You can put really whatever sort of values you want in here. Just make sure that all the values are of the same data type, okay?

So if you’re working with whole numbers, make sure they are all whole numbers and no decimal. So we can’t, for example, have 15.2. That’s not really gonna work. So they all have to be the same type. And that is how we create an array. Now what are arrays useful for? Why do we need this? Why can’t we just create various different variables, for example, num1, num2, num3, so on and so forth?

Well, the benefit of having arrays is that they are very flexible, and especially when tied into software, like Roblox for example, we can get an array of players. So we can get a list of all of the players in game and then enact some commands or actions upon them by just going, “Okay, for all of these players, do this.”

Or we could be looping through a number of different objects. For example, if we have a bunch of lights in our scene and it turns nighttime and we want these lights to turn on, we could have an array that contains all of the lights inside of our game and just have a function that loops through all of those array elements and turns them on.

Now let’s go over a bit of terminology. With arrays here, we have two things. We have an element and we have an index. So first of all, an element is basically a value inside of an array. So this numbers array right here has four elements: 5, 10, 15, and 20. Each of these is known as an element, an element of an array.

And an array is made up of a number of different elements. Now what is an index? Well, an index is basically the number used to identify a specific element within an array. So let’s just say what we want to do is when we play the game, we want to print, for example, the third element of the array to the console. So we can see 15 pop up in chat.

Well, what we can do then is go down here, go print, and we enter in what we want to print. So how do we print the third element of this numbers array? We can’t go numbers, like so. That will print everything.

So what we need to do is after we write down the array, we need to put down two square brackets, like so, and specify the index, and we are gonna go three, because in Lua, the first element of the array has an index of one, then two, then three, then four.

So if we wanna get the third element of the numbers array, we need to give it the index of three. So hopefully, what this line of code here should do is print out the number 15. So let’s press play and see if that happens. If you don’t have the output window here, you can go over to the View tab and just click on Output right here to enable that.

So let’s press F5 and see if it works. All right, and as you can see over here in the console, it says 15. So it’s printing out the correct number. Let’s go Shift + F5 to stop playing. And for example, if you want to print the first element of the array, you’d go numbers[1]. If you wanted to print the fourth element, you’d go numbers[4] and so on and so forth.

Now as well as being able to read the values, like we are doing here, I’m printing them out to the console, we can also change the values. So for example, let’s just say we wanna change the first value of this array and we want to change it to, let’s just say a thousand. So we can go print out the first element of the array here.

And then what we can do is just go numbers[1]. And what we can then do is just like with a normal variable, we can go = 1000, like so. So what we’re doing is we are defining the array here, we are changing the first element of the array to be 1000. and then we are printing that out to the console.

So when we press play, we shouldn’t see five pop in the console, but we should see 1000. So let’s press F5 and see if that works. And as you can see right here, it says 1000. So that’s working just fine. And yeah, you can go along, you can change the various different elements of your array, but what happens if you want to, for example, add more?

Let’s just say you’ve defined your numbers, but down the track, you’re like, “Okay, I wanna add more numbers to this array. How do I do that?” Well, it’s actually quite simple. What you need to do is, first of all, let’s go ahead and not change one to 1000. Instead, what we’re gonna do is we’re gonna go square bracket.

And right now, the max index we can choose is four because there’s one, two, three, four elements in our array. But what if we entered in five instead? We don’t have an element with the index of five. So what that means is we can define a new one. So numbers[5] = 25. And then let’s log that to the console.

So we can print that out in the output down here. And let’s see how that goes. F5. And there we go, 25. So creating new elements inside of our array is pretty easy. We just define a new index for that number and have it equal to a value. So we’ve got all this here, now how about different data types?

So for example, if we’d go down here and create a brand new local array, and let’s just call this one strings. This is gonna be equal to a new array here, and we can have words. For example, we can have Roblox. And make sure to have these inside of quotation marks. We can have Roblox, we can have Studio. Oops.

We can have Studio. We can have Tutorial, for example. And it’s gonna act in the exact same way. So let’s go print and let’s go strings, and we wanna print out, let’s just say we wanna print out Studio, so we’d give it the index of two. Press F5 to play. And we should see, there we go. It says Studio here in the output. So yeah, it works with strings.

It also works with Booleans as well, so true and false values. Pretty much any type of variable that you can create normally can also be inside of an array. That includes parts, that includes players. That includes even scripts, for example. Pretty much anything in Roblox can be put inside of an array, just like it can be put inside of a variable.

All right. So in the next lesson, we are going to be having a look at how we can actually start putting some of our Roblox-related objects inside of arrays and what we can do with that information.

We’ll be looking at putting players in arrays. We’ll even be looking at going through our Workspace and putting specific things inside of an array. So thanks for watching, and I’ll see you all in the next lesson.

Interested in continuing?  Check out our all-access plan which includes 250+ courses, guided curriculums, new courses monthly, access to expert course mentors, and more!