What's the difference between an array and a table?

Very simple question:
What’s the difference between an array and a table?

I’ve seen it used differently, with the API reference stating that it needs an array to be given for a function, and sometimes it needs a table, and yet I can’t seem to find a difference? I’m probably just losing my braincells, but I’m surprised I do not know this, since I started last December.

Thanks.

1 Like

There isn’t really a difference, those 2 are basically the same. If you hear someone say this is a table or that is an array etc, they are talking about the same thing pretty much. A datatype that can carry multiple data types like Strings, Integers and Booleans.

Tables is a native datatype that can store values assigned to a non-nil value keys.

Arrays are referring to tables that only stores values using numeric value keys.

6 Likes

Array = table where the indexes are numbers
Dictionary = table where the indexes are strings (or anything that isn’t a number.)

local array = {
  "String one";
  "String two";
}

print(array[1]) --Expected: "String one"

local dict = {
  ['weapon'] = "Sword";
  ['armor'] = "Silver";
}

print(dict['weapon']) --Expected: "Sword"
print(dict.weapon) --Expected: "Sword"
--Notice how we can use path notation to index keys from our dict, where with the array we have to use a number.
6 Likes

These terms are often used interchangeably, even though they are not the same thing.

In general programming, an array is defined as a collection of data, all of the same type and with a set number of values. Eg.

int array {40, 30, 20, 10, 0}

This will create an array with a length of 5, and initialize array[0] to 40, array[1] to 30, and so on.
With these kinds of arrays, refering to or attempting to set array[6] would result in an error. If you created an Int array with a string value included, it would also error. Eg.

int array {6, “string”} // This is not an integer value, so my program won’t work!

Roblox does not use typical arrays, it uses a native datatype called a “table”. A Roblox table is still a collection of data, however a table does not have a set number of values on creation and can hold any different types of data in the same table. Eg.

local thisTable = {40, “30”, 20.5, 10, false}

In Roblox, you can refer to thisTable[6] and nil will be returned, or you can set thisTable[6] to a value. There are also other similar datatype such as dictionaries and tuples, you can read up more on them on the web.

1 Like

The amount of overexplaining in this thread is crazy.

A table is a data type that can store other data. They are often used to represent objects. Data within a table is accessed using a key. Keys are often strings. A car table, for example, might contain a key “color” that points to the value “red”.

An array is a table where the keys are integers and they’re in order starting at 1. Arrays are often used when the data within the table isn’t standardized. For example, a car table might have a key “PossibleColors” that points to an array “{“red”, “blue”, “green”}”. GetChildren and similar functions return arrays. When you have an array, you normally access the data by iterating over its contents instead of using keys to access it directly.

10 Likes