For each item in list - variables not storing


I’m trying to set a series of variables using a “For each item j in list” block. While the loop is running, I can see the integers being generated. However, at the end of the loop, all the variables are set to 0. What am I doing wrong? How can I get the variables to retain their assigned values after the loop completes?

Screenshot 2023-06-08 at 2.25.22 PM

Your blocks set a single variable to a random number each iteration of the loop. So j is only going to have the last value that was set when the loop ends.

Instead of storing every value in the same loop variable (variables like j are really only to be used inside of a loop block), store the random integer as a list item. Use the insert at last block (Lists Blocks - Thunkable Docs) from the List drawer to add an item to the end of the list during each iteration of the loop. You actually don’t need all of those separate variables. You can just store the list in a single variable that is a list (initialize it as an empty list or set it to an empty list when the button is clicked. Create a variable called something like valuesList and then use that with the insert at last block.

If you want to generate 6 random numbers and store them, you don’t need to use the for each item block. You can just use the count with i from 1 to 6 block.

Awesome thank you!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.