How to toggle a while loop/ reuse a return function?
Asked by
5 years ago Edited 5 years ago
Please note my scripting abilities are still limited.
The code I have posted below places a table into a fake array and randomly displays a table key and its value, it will continuously search through the entire array until a desired number is found.
Upon finding a desired number the script intentionally breaks its loop cycle.
Unfortunately, I would like to be able to reuse this function at all times and trigger it when necessary.
I suspect that the code needs some rewritting and you can't simply use an if statement trigger because of the while loop breaking. What would my best approach be?
15 | local function GenerateRandomIndex() |
18 | for index in pairs (Player 1 Grid) do |
19 | table.insert(Array, index) |
22 | local RandomNumber = math.random( 1 , #Array) |
23 | return Array [ RandomNumber ] , Player 1 Grid [ Array [ RandomNumber ] ] |
26 | local ObjName, ObjValue = GenerateRandomIndex() |
29 | print ( "The random grid tile is" , ObjName, "The value is" , ObjValue) |
32 | print ( "The selected grid tile was invalid, trying again." ) |