It’s rife with syntax, logic, and scope errors.
Might I suggest spending some time on basic Lua tutorials?
In my longer live streamed video, I show how to use SITL for testing. You should use that method rather than shooting in the dark and then asking me how the next half-baked iteration looks.
You are jumping into the deep end, and I fear the script you’ll create might end up verging on dangerous.
I am busy with travel this week and replying from my phone. It’s difficult at best to give you feedback.
@iampete, maybe you can offer some guidance?