Script modules - script executes properly... but only once

In the scrip modules editor I defined a new script module named ‘buildquery’. The script sets the value of a client tag to true and prints ‘set bit to 1’ to the output console. That is, the module definition includes:

import system system.tag.write('[Client]TestBit',1) print 'set bit to 1'

In my project I have a button component which calls the ‘buildquery’ script in a mouse clicked event script. I call the script using:

app.buildquery print system.tag.read("[Client]TestBit").value

First I zero the client tag then click the button. The output console reads:

set bit to 1
True

The script executes properly. But… if I click the button again the output console only reads:

True

If I zero the bit and click the button again then the output console reads:

False

If I go into the script module editor, select the buildquery script and hit ‘Apply’ and then go back to my project and click the button the console reads:

set bit to 1
True

The script executes properly again, but a second click only generates:

True

in the output console. I get the same behavior if I run the script in the script playground. Why does the script only execute properly the first time it is called after being applied? Am I missing something here?

I tried defining a function containing the same script:

def testbit(): import system system.tag.write('[Client]TestBit',1) print 'set bit to 1'

Now if I call the testbit function I get the expected result every single time. Is this normal behavior? Shouldn’t it work without defining a function?

No, absolutely not. The script modules are there for you to define re-usable functions that you want to use across your project.

The fact that it seemed to work once is merely because each time a module is loaded (first time its imported/referenced) its globally scoped code is executed.