Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

What does [i] mean?

Asked by
Yeevivor4 155
10 years ago

I wonder what ** [i]** mean. Can anyone explain how this works?

2 answers

Log in to vote
2
Answered by
gskw 1046 Moderation Voter
10 years ago

In a for loop, you often have a variable called i. Example:

local table={"Hi", "there", "everyone!"}
for i=1,#table do
    print(table[i])
end

In the for loop, you have a variable called i. It is a counter that is incremented every time the for loop runs until i is greater than #table, which is the number of variables in table. You use [i] to reference the ith member in table.

Ad
Log in to vote
1
Answered by
duckwit 1404 Moderation Voter
10 years ago

To add to chararray256's answer:

[i] on it's own doesn't mean anything, but square brackets are syntactical for indexing tables, i.e. accessing elements of the table that are stored with certain keys.

Take for example this code:

myTable = {"Hello", 7, Instance.new("Part")}
print(myTable[1])
print(myTable[2])
print(myTable[3])

The output would read:

Hello
7
Part

Keys don't have to be numbers though, they can be strings as well!

Answer this question