Generic loops are used for keys and values. For example:
local Dictionary =
{
John = "Doe",
Jane = "Doe",
Hamburger = "Hoovy"
}
for i, v in pairs(Dictionary) do
print(i, " ", v)
end
Output:
John Doe
Jane Doe
Hamburger Hoovy
If you only print the keys (i):
for i, v in pairs(Dictionary) do
print(i)
end
Output:
John
Jane
Hamburger
Since you’re using a regular 1D array (One dimension) which is a table. Use a numeric for loop to go through all elements as follows:
for i = 1, #Table do
print(Table[i])
end
> Output:
> 1
> 2
Breaking down that code, you create the variable i and set it to 1. Then, you loop until i is equal to the number of entries in the Table (#Table) which is 2. So in reality, the loop looks like this:
for i = 1, 2 do
print(i)
end
Since i is incrementing by 1 every time it loops, Table[i] will be Table[1] or Table[2] in this case. Adding a third number in the Table array will have your loop iterate 3 times. Which means the possible values will be Table[1], Table[2], Table[3]
You can set how much you want i to increment by as well. For example:
for i = 1, #Table, 2 do
print(i)
end
It will only print 1, because before it loops again, you incremented i by + 2, which means it’s greater than #Table which is 2 (Number of entries in the array). So it loops once.
For your problem, your while loop is forever looping on the first loop, since there’s no condition to break while loop for it to proceed to the next value. You could, however, do the following:
for i, v in pairs(Table) do
v = 2
while true do
v = v + math.random(1, 5)
print(v)
if v == 5 then
print(v.."is 5!")
v = v - 6
break -- Exits the loop if this statement is true
end
if v<0 then
print("An error has occured, negative number.")
break -- Exits the loop if this statement is true
end
wait(1)
end
end
This will allow you to iterate through the whole Table array.