what are, _, in
for _, v in pairs() do
and what are,
Value[1] or [1] or [Picture]
for?
and last but not least, how do you use tables like,
table.create()
?
what are, _, in
for _, v in pairs() do
and what are,
Value[1] or [1] or [Picture]
for?
and last but not least, how do you use tables like,
table.create()
?
The first parameter _
is the index of the table currently in the loop, so if the loop is on the 2nd thing of the table, it is 2
, and so on
When you do Value[1]
, you’re getting the first value in a table. When you see people use [Picture]
for example, it’s typicall used with Dictionaries, the fact that it’s not in quotation marks means it’s a variable with the name of a key in a dictionary
table.create()
works by giving it 2 parameters, the count and the value. To explain how it works, here’s an example
local tbl = table.create(3, "Embat")
print(tbl) -- prints {Embat, Embat, Embat}
If you have any questions, feel free to ask!
for _, v in pairs() do
For every element in the table provided in the pairs(here), it does something. i is the index or key. If your table is like this:
{[“idk”] = “idk”}, then i means the string in the square brackets.
But if it’s like this {“!”, “hello”, “foo”} then, i would be the index. ! index is 1, hello index is 2, foo index is 3. you get it.
Table[index number] gets the value of an index in an array (no keys, just indexes).
Table[key name no brackets] gets the value of the key in the table,
table.create() creates a table of values
local randomthing = table.create(10 --[[ how many times it's going to add that -]]--, "RFL" -- the item to be repeated 10 times)
ex:
randomtbl = table.create(5, "Foo") -- {"Foo", "Foo", "Foo", "Foo", "Foo"}
print(table.concat(randomtbl)) --FooFooFooFooFoo
Example of key and index manipulation
local OurArray = {"RFL890", "EmbatTheHybrid", "builderman", "Telamon"}
local OurDictionary = {
["Cash"] = 56789;
["Gems"] = 12;
}
print(OurArray[2], OurArray[1], OurArray[4]) -- EmbatTheHybrid, RFL890, builderman
print(OurDictionary[Gems]) -- 12
print(OurDictionary[Cash]) -- 56789
-- Other ways to get stuff from Dictionaries:
-- Dictionary["KeyName"], Dictionary.KeyNameOnlyIfItHasNoSpaces
-- Other ways to make a dictionary:
-- {Person = 58}
-- then access by dictionary[Person] or diction["Person"] or diction.Person.
Do note that those aren’t parameters but variables. Value[1]
will get you the value of the first index in Value
, not it’s index.
in lua we basically define enumerable loops as for i,v in enumerationFunction do
basically _ is just sugar syntax to mean we wont use this but it functions the same as i.
Value[1] or [1] or [Picture]
It really depends on the context however we usually append the brakets [] to a array to get the element in it. So like in other words your
{"a", "b", "c"}
Is actually just a simplfied version of its actual form
{
[1] = "a";
[2] = "b";
[3] = "c"
}
with the or im not sure, it could be used as part of a variable statement. Meaning if value[1] is nil it will be [1] and if thats nil it will be [Picture]
Not really sure with table.create() man its probably a legacy feature I would guess it’d be like
{"a", "b", "c"}
same as
table.create("a", "b", "c")
The _ is a variable, most of the times people uses this its because they dont feel it will be useful for the code, it can be named anything, however its definitions will be:
for index, value in pairs(tbl) do
end
You can rename index to anything just like value, but they will be defined as: the index of the current value in the iteration and the value of the current index in the iteration, as an example:
local greetingsTbl = {"Hello!", "Hi!", "Hey!"}
for i,v in pairs(greetingsTbl) do
print(i,v) -- It will print 1 Hello! 2 Hi! 3 Hey!, thats because its printing the index of the value and the current value in that index.
end
When you do Value[1] it is going to refer to the value in the index 1 of that table, and when you do Value[Picture] it is going to refer to the value in the index Picture of that table.
Again with the example
local newtable = {"first", Greeting = "hi", ["Greeting2"] = "hey"}
print(newtable[1]) -- first
print(newtable["Greeting"]) -- hi
print(newtable["Greeting2"]) -- hey
table.create() is a function that is the same as manually creating a table, except that its first parameter is the ammount of times the value thats going to be the second parameter id going to be put into it.
local newtable = {"ok"}
local anothertable = table.create(1, "ok")
-- if you use more numbers in the table.create()
local newone = table.create(5, "hi")
print(newone[1])
print(newone[2])
print(newone[3])
print(newone[4])
print(newone[5])
-- These all are going to print hi
You can find out more in those 2 articles:
Tables | Roblox Creator Documentation for a better understanding of arrays and dictionaries (basically tables)
table | Roblox Creator Documentation for a list of table functions and what they do