Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What does [i] in Lua mean?

I always see them in free models when attempting to emulate their code. What does the [i] syntax represent?

1 comment

0

Thank you very much. I have been looking for this for a long time.

1 answer

✓ Accepted answer

Brackets after an object/table index on that table/object.

Something you may be more familiar with that indexes is the dot (.).


When you do game.Workspace, Lua interprets it as game["Workspace"]. That is, your asking for the value as the index "Workspace" (the string "Workspace").

Equally valid would be the following:

index = "Workspace"

ws = game[index]

-- or

i = "Lighting"
lights = game[i]

Any value except nil can be used as an index in a table. tab[i] is asking for the value at the index i. i will usually be a number, as when going through a list; for example, tab[1] asks for the first thing in the list tab, tab[2] the second, and so on.

If you had a loop, you could use tab[i] to the the ith element out of tab --- this is especially useful when i is the iterator in a loop.

Here's an example:

primes = {2, 3, 5, 7, 11, 13, 17, 19}

for i = 1, #primes do
	print("i is",i)
	print("so primes[i] is primes[",i,"] = ",primes[i])
	print(" ")
end

Which gives the following output:

 
i is	1
so primes[i] is primes[	1	] = 	2
 
i is	2
so primes[i] is primes[	2	] = 	3
 
i is	3
so primes[i] is primes[	3	] = 	5
 
i is	4
so primes[i] is primes[	4	] = 	7
 
i is	5
so primes[i] is primes[	5	] = 	11
 
i is	6
so primes[i] is primes[	6	] = 	13
 
i is	7
primes[i] is primes[	7	] = 	17
 
i is	8
primes[i] is primes[	8	] = 	19

This discussion is preserved as it was. New answers, comments, and votes are closed.