When I started with Lua, like you, I wrote a program with thousands of states, had the same problem and thoughts, until I realized that I was doing this completely wrong :)
Lua has coroutines and threads, you need to use these functions to do what you need. They may be a little complicated at first, but you should understand them in a few days, it will be well worth your time.
source
share