xxxxxxxxxx
-- for in/for loops
for key, value in pairs(table) do
-- If value is a table then you can do another iteration tho, for in for in isn't recommended.
end
for key, value in next, table do
-- Same as above.
end
for variable = 0, 1, 1 do
-- The variable is automatically set to the first value you input after "=".
-- The variable is then incremented by the third value you input after "="
-- until it reaches the second value you input after "=".
variable = variable + 1 -- The above only works if you increment it,
-- else it infinitely loops through the block.
end
while true do
-- Infinite loop
end
xxxxxxxxxx
-- For K,V in table
for k,v in pairs(tbl) do
print(k)
print(v)
end
-- For i=0, num
for i=0, num do
print(i)
end
xxxxxxxxxx
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"
xxxxxxxxxx
for <init>,<max/min value>, <increment> [default is 1]
do
statements
end
-- Example
for i=1, 5, 1
do
print(i)
end
--Output
1
2
3
4
5
xxxxxxxxxx
--[[
There are two types of lua for loops.
There is the generic definition, (pseudo-)expression, increment,
and there is one that allows the use of iterators.
]]
-- The first kind can be used like this:
for a = 0 --[[Define a as 0]], 10 --[[Continue until a reaches 10]], 2 --[[Increment by 2 each iteration]] do
print(a); -- 0, 2, 4, 6, 8, 10.
end
-- The second kind requires an iterator. There are two commonly used built-in ones.
-- pairs and ipairs.
-- pairs uses the built-in next function, which gets the next key in a table given a previous key.
-- pairs can be used both for pure arrays and non-numerical indices (ie. maps).
for i,v in pairs({["A"] = 5, ["B"] = 10}) do
print(i, v); -- A 5, B 10.
end
-- ipairs is different in that it can only loop over tables with numerical indices (ie. arrays) hence the name *i*pairs.
for i,v in ipairs({5, 10}) do
print(i, v); -- 1 5, 2 10.
end
-- You can read more about iterators here:
-- https://www.lua.org/pil/7.3.html
xxxxxxxxxx
while true do
-- Put what you want looped in here
-- Do mind that this doesn't work for roblox and for that you should be using the roblox devforum.
end