The issue in the particular file you sent is because you call the function before the array is populated, the function call should be after the array load.
However your original screenshot didn't seem like it had this issue so i didn't mention it, because you force the dialogue with a key press when the array should already be populated.