i have this script that goes as followed:
“for i = 1, #children do
children[i].material = “Neon””
my first question is what does “for i = 1” do? and my second question is what do the [] brackets do?
i have this script that goes as followed:
“for i = 1, #children do
children[i].material = “Neon””
my first question is what does “for i = 1” do? and my second question is what do the [] brackets do?
put three backticks please, it’s under the Esc key, also it’s to loop until it reaches a number after 1,
, and the brackets []
are for getting a certain item in a table using a string or a integer
This is making a for loop. The code inside will run multiple times, and the i
variable will be set to a different number each time. The number starts at 1 and ends at another number.
for i = 1, 5 do
print(i)
end
Output:
1
2
3
4
5
You can get the length of a list like this:
local list = {"a","b","c","d"}
print(#list) --> 4
-- The '#' operator can get the length of a list and a string
You can get items of a list like this:
local list = {"a","b","c","d"}
print(list[3]) --> c
Basically, it iterates through all the children and changes their material. In Lua, arrays start from 1. So it starts at 1 and ends at the length of the array(which is given by the # operator). More specifically this is equivalent to the following code:
for _, v in pairs(children) do
v.Material = "Neon"
end
The only difference is that instead of v
you have children[i]
which is pretty much the same(the value at a specific index). The _
is the i
but in Lua, it’s a good practice to name it _
when it’s not being used.
Tables can be very complicated to understand at first, but once you get it you’ll love it. All you have to understand is how to access stuff in a table. Roblox has two types of tables, arrays and dictionaries.
The array is simple, you just type
local array_value_2 = "hello world"
local my_table = {"array value 1", array_value_2, workspace.Part, function()end, {}}
The array has a hidden element which is the index. Every index is separated by a comma and the first index is 1, so “array value 1” has index 1, workspace.Part has index 3.
This is what makes the array and dictionary different. In a dictionary, the “index” is called a key, it is not hidden. For example here’s a dictionary:
local array_value_2 = "hello world"
local my_table = {key_1 = "array value 1", key_2 = array_value_2, key_5 = workspace.Part, key_69 = function()end, empty_table_key = {}}
The key and index are the same thing, don’t get confused by that they are called differently. The index is a key, but not every key is an index just like how a square is a rectangle, but not every rectangle is a square.
These keys are how you access the contents of a table. Another very helpful thing is to think of the Explorer as a table. For example:
local rainbow_part = workspace.Parts_Folder.Rainbow_Part
This code tries to access an Instance in the explorer ||(everything in explorer is Instance, but the first ones like Workspace, ServerScriptService, etc. are services, that’s why you do game:GetService("")
)||
In the explorer every object DIRECTLY inside another object is its child. Every object INDIRECTLY inside another object is its descendant. For example in the code above Rainbow_Part
is a child of Parts_Folder
, but it’s a descendant of Workspace
because its an indirect child. This is applicable to the tables you make in scripts.
What happens there is the key of workspace.Parts_Folder
is Parts_Folder
and the value is what would be returned if you assign it to a variable.
I haven’t talked about values above so every key and index has a value. When you try to explore a table, the key/index that you choose to access returns the value assigned to it. For example:
local array_value_2 = "hello world"
local my_table = {"array value 1", array_value_2, workspace.Part, function()end, {}}
local value = my_table[1] -- this is now equal to "array value 1"
It’s incredibly important to understand what return
does when you work with tables because they return values. When you assign a variable to anything, it’s now equal to whatever that returned (as seen above).
With dictionaries you can use different ways to select keys in a table, for example you can do the [] with a string inside, use a dot .
or use a colon :
which is also called a method. The colon basically adds a bit more complexity as it passes an additional parameter called self
so you don’t have to. I never found a use for methods and you probably won’t for a long time too. For example:
local my_table = {key_1 = "array value 1", key_2 = array_value_2, key_5 = workspace.Part, key_69 = function()end, empty_table_key = {}}
local value_1 = my_table["key_1"]
local value_1 = my_table.key_1
The values returned by a table can also be functions, but when you try to access a function in a table with ()
it runs the function with the given paraments. For example:
local function_table = {
function_1 = function()
return "one"
end,
function_2 = function()
return 2
end,
}
local value_1 = function_table.function_1() -- value_1 = "one"
local value_2 = function_table["function_2"]() -- value_2 = 2
With some basic function understanding you can also pass parameters that change what the function returns, but this isn’t the current topic.
Long story short, the square brackets are a good way to search a table if you want to make your code more abstract. The circle brackets are to call functions and pass parameters, the curly brackets are to make tables. For example:
local function_table = nil
function_table = {
function_1 = function(key_name)
return function_table[key_name]
end,
function_2 = function(key_name)
return function_table[key_name]()
end,
function_3 = function()
return "hello_world"
end,
}
print(function_table.function_1("function_3")) -- prints the function itself
print(function_table.function_2("function_3")) -- prints "hello_world"