Scripting doesn’t operate in real time like expressions, it’s more suited to building and manipulating comps, layers, etc. in more of a batch environment.
You may still be able to use an expression for what you’re trying to do, you just need to work around the “expressions have no memory” constraint. What is it, exactly, that you want to do?