for table lua code example

Example 1: lua for each in table

for k,v in pairs(table) do
 -- k is key
 -- v is value
end

Example 2: For loop lua

for startValue, EndValue, [increments] do
        --code to execute
end
--The increments value is optional.  If it isn't defined, it is assumed to be "1"

Example 3: how to make a table in lua

--You need to use this {} and put them in a varible
local Table = {13,"1hihihi",
-- u can even do tis
{122,222}}

Example 4: table lua

a = {}
    x = "y"
    a[x] = 10                 -- put 10 in field "y"
    print(a[x])   --> 10      -- value of field "y"
    print(a.x)    --> nil     -- value of field "x" (undefined)
    print(a.y)    --> 10      -- value of field "y"

Example 5: lua table

local Table = {
	"Hello",
	"Goobye",
	"Bruh",
	Part = Instance.new("Part")
}
-- This is our table!

print(#Table)
--[[
This will print the amount of stuff inside the table by numbers!
For example, if you had 140 objects inside a table (doesn't matter if it's a
string) then print(#Table) will print out the number 140, exactly how many
things are inside the table!

Output: (the amount of objects inside the table BY NUMBERS)

]]

for i, v in pairs(Table) do
	print(i,v)
end
--[[
This is a for loop. It will go through each and every object inside the table.

i is the index. The index is like the line number, where the object is located.
Let's say that Bob is the 4th object in the table.
If we would think of tables as lines, Bob would be at line 4.
So if we'll do print(i) it would print out the line where Bob is.
Output: 4
As long as you use a for loop to get every object!

v is the object itself.
Let's say yet again that Mark is inside the table.
so v would be counted as Mark himself.
if we would do print(v) it will print out the name,string etc. of the object!

Output: Mark
]]

Example 6: for loop lua

local t = {}
for key, value in pairs(t) do
	print(key, value)
end

Tags:

Lua Example