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

**URL:** <https://forum.inductiveautomation.com/t/script-modules-script-executes-properly-but-only-once/7176>\
**Category:** Ignition\
**Created:** [September 1, 2013, 12:23am UTC](https://forum.inductiveautomation.com/t/script-modules-script-executes-properly-but-only-once/7176 "2013-09-01T00:23:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamthebull](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@iamthebull](https://forum.inductiveautomation.com/u/iamthebull)\
**Post date:** [September 1, 2013, 12:23am UTC](https://forum.inductiveautomation.com/t/script-modules-script-executes-properly-but-only-once/7176/1 "2013-09-01T00:23:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![iamthebull](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@iamthebull](https://forum.inductiveautomation.com/u/iamthebull)\
**Post date:** [September 1, 2013, 6:06am UTC](https://forum.inductiveautomation.com/t/script-modules-script-executes-properly-but-only-once/7176/2 "2013-09-01T06:06:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [September 1, 2013, 3:19pm UTC](https://forum.inductiveautomation.com/t/script-modules-script-executes-properly-but-only-once/7176/3 "2013-09-01T15:19:11Z")

</div>

> [@iamthebull](#):
>
> 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.
