For I, v problems

so recently I’ve been starting over and my code and starting to write smaller to try to learn more. While doing so I wrote some code that takes the v value from a table, and adds a random number to it. While doing so I wrote some code that detected when v reached 5 to -6 from v causing it to become a negative number and top print something but it doesn’t do that, it just skips over the code.

code:

Table = {1, 2}

for i, v in pairs(Table) do 
	v = 2
	while true do
		v = v + math.random(1, 5)
		print(v)
		wait(1)
	end
end

for i, v in pairs(Table) do
	
	if v == 5 then
		print(v.."is 5!") 
		v = v - 6
	end
	
	if v<0 then
		print("An error has occured, negative number.")
	end
end
1 Like

you can also use for i = 1,#Table do and replace v with Table[i].

or maybe place

if v == 5 then and if v <0 then up side down

what does 1 mean and what does

do?
also what does

mean?

#Table = how many values are there in Table
Then 1 … I don’t know, they are often equals to 1. So you don’t have to know because I don’t know too.
Table[1] = 1
Table[2] = 2

Example:
T = {1,9,5,6,7,0}
then use a for loop to check everything in the T
T[1] = 1
T[Position] = Value inside the position of T
In other word i = how many times it has been looped

#Array returns the number of items within an array, and for i = 1 is used because the first index of an array in Lua begins at 1. Even then, him using a generic for’s fine.

@Hamburger_Hoovy The reason why it’s not carrying over’s because you’re overwriting the v variable for the first for loop’s scope; you’d need to either overwrite the index within the array, or add the v + Number to a separate array, then iterate through that said separate array. Not sure how I missed the while loop lol.

The while true do loop nested inside the pairs loop is causing the script to hang when it reaches the first item in the table.

1 Like

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.

1 Like

This doesn’t work, when v = 5 it doesn’t do v - 6 which in return prompts if v<0 to never respond.

Also what is an array and index?

not the best at scripting :neutral_face:

Just wrap it around a coroutine:

for i, v in pairs(Table) do 
	v = 2
    coroutine.wrap(function()
	    while true do
	    	v = v + math.random(1, 5)
	    	print(v)
	       	wait(1)
	    end
    end)() -- Make sure to call it!
end

Basically it creates a “Script” inside of the script, and it won’t yield the current thread.

This causes the numbers to go backwards and in 2 number increments. Basically

Output:
6
8
1 second passes* 
9
4
and so on
for i = 1, #Table do -- Start from the first element in the table, to the last
    local num = Talbe[i] -- Grab the next value in Table
    while num >= 5 or num <= 0 then -- While num is higher or equal to 5, or, lower or equal to 0,
        num = num + math.random(-5, 5) -- Add any number between -5 and 5
        wait(1)
    end
    if num > 5 then
        print(i.." is higher than 5")
    elseif num == 5 then
        print(i.." is 5!")
    elseif num  == 0 then
        print(i.." is equal to 0")
    else
        print(i.." is a negative number, an error has occured")
   end
end

You can play around with the conditions to break the while loop. Or, like TOP_Crundee said, start a separate process with a coroutine. However, it could became very inefficient depending on the size of the array.

Array is just another word for table.
Index in an array (table) standpoint means which element you are pointing to inside the array.

local Table = {"Hello", "HelloTwo"}
print(Table[2]) 

[2] is the index, so you’re grabbing the second value in Table. Which is “HelloTwo”