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?
Player1Grid = { ["A1"] = 1, ["A2"] = 2, ["A3"] = 3, ["A4"] = 4, ["A5"] = 5, ["A6"] = 6, ["A7"] = 7, ["A8"] = 8, ["A9"] = 9, ["A10"] = 10 } while wait() do -- Perform loop and wait function to prevent fatal lag. local function GenerateRandomIndex() local Array = {} -- Prepare a fake array. for index in pairs(Player1Grid) do -- Gather table information. table.insert(Array, index) -- Insert the index into array table. end local RandomNumber = math.random(1, #Array) -- Get a random number of array. return Array[RandomNumber], Player1Grid[Array[RandomNumber]] -- Return both the name and number. end local ObjName, ObjValue = GenerateRandomIndex() -- Assign appropriate data to the variables. if ObjValue == 3 then -- Compare the result with a string value. print("The random grid tile is", ObjName, "The value is", ObjValue) return -- If the desired value is found print it and break the loop. else print("The selected grid tile was invalid, trying again.") end end
function place()--Makes it a function Player1Grid = { ["A1"] = 1, ["A2"] = 2, ["A3"] = 3, ["A4"] = 4, ["A5"] = 5, ["A6"] = 6, ["A7"] = 7, ["A8"] = 8, ["A9"] = 9, ["A10"] = 10 } while wait() do -- Perform loop and wait function to prevent fatal lag. local function GenerateRandomIndex() local Array = {} -- Prepare a fake array. for index in pairs(Player1Grid) do -- Gather table information. table.insert(Array, index) -- Insert the index into array table. end local RandomNumber = math.random(1, #Array) -- Get a random number of array. return Array[RandomNumber], Player1Grid[Array[RandomNumber]] -- Return both the name and number. end local ObjName, ObjValue = GenerateRandomIndex() -- Assign appropriate data to the variables. if ObjValue == 3 then -- Compare the result with a string value. print("The random grid tile is", ObjName, "The value is", ObjValue) return -- If the desired value is found print it and break the loop. else print("The selected grid tile was invalid, trying again.") end end end--End of the function place()--Call this so often as you want.
You can call the place function as often as you want now.
I hope this helped and hope you will be a great scripter, one day!