# Variable global in event script doesnt work

**URL:** <https://forum.inductiveautomation.com/t/variable-global-in-event-script-doesnt-work/71495>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [March 23, 2023, 2:36pm UTC](https://forum.inductiveautomation.com/t/variable-global-in-event-script-doesnt-work/71495 "2023-03-23T14:36:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michal\_Goralczyk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michal_goralczyk/32/55564_2.png) [@Michal\_Goralczyk](https://forum.inductiveautomation.com/u/Michal_Goralczyk)\
**Post date:** [March 23, 2023, 2:37pm UTC](https://forum.inductiveautomation.com/t/variable-global-in-event-script-doesnt-work/71495/1 "2023-03-23T14:37:00Z")

</div>

Hello,

I have a simple script in an event script that when the button is pressed, causes the following code to write to the Label text component:

```auto
def runAction(self, event):
	tempCounter = 0
	
	def funkcja():
		global tempCounter
		tempCounter += 1
	    
	funkcja()
	self.getSibling('Label').props.text = tempCounter

```

I get this error:

Error running action 'dom.onClick' on Cameras/Cameras\_test\_2@D/root/FlexContainer\_2/Button\_1: Traceback (most recent call last): File "function:runAction", line 8, in runAction File "function:runAction", line 6, in funkcja NameError: global name 'tempCounter' is not defined

Script console work fine:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/e/8edef6519093c2ad49b107d1f878a6901e6a8883.png)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 23, 2023, 2:45pm UTC](https://forum.inductiveautomation.com/t/variable-global-in-event-script-doesnt-work/71495/2 "2023-03-23T14:45:18Z")

</div>

The `global` keyword in `funkcja` marks that name as being outside of both `runAction` and `funkcja`, but `runAction` itself is the scope for your `tempCounter`. You might be able to include a matching `global` statement in `runAction`. Seems absurdly fragile.

Why do you need this code? What are you really trying to accomplish? (Also, the interactive interpreter is **never** a suitable place to test code intended to be used in Perspective. The scopes are not compatible.)

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 23, 2023, 2:47pm UTC](https://forum.inductiveautomation.com/t/variable-global-in-event-script-doesnt-work/71495/3 "2023-03-23T14:47:23Z")

</div>

You're probably better off using a gateway tag (if the value should be shared across all sessions) or a custom session property (if it's different for each login).

---

<div class="post-metadata">

**Author:** ![Michal\_Goralczyk](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michal_goralczyk/32/55564_2.png) [@Michal\_Goralczyk](https://forum.inductiveautomation.com/u/Michal_Goralczyk)\
**Post date:** [March 23, 2023, 2:53pm UTC](https://forum.inductiveautomation.com/t/variable-global-in-event-script-doesnt-work/71495/4 "2023-03-23T14:53:20Z")

</div>

I understand the problem I will try something else thank you for the tip
