Missing end statements should be one of the easiest things to find, if your code is indented properly. Looking at the first screenshot everything looks okay, but if you look at the second screenshot an indention error was leading you to trouble.
Look at the line: local whereFrom = math.random(3)
It is indented properly but just below it the “if” statement is tabbed to the left a level. Since that whole block is shifted to where it looks like it lines up with the function declaration, the last end looks like it ends the function. Had that if block been indented one more level, you would have easily seen where the missing end was.
Parring up block beginning and endings is one of the most frustrating things to new programmers. Even experienced developers can get frustrated by them too. But being disciplined with your code formatting is a great way to avoid a lot of common pitfalls.
Rob