# \[SOLVED\] \[ENGINE ONLY\] Start Stop script when changing a scene

**URL:** <https://forum.playcanvas.com/t/solved-engine-only-start-stop-script-when-changing-a-scene/29170>\
**Category:** Help & Support\
**Tags:** scripting\
**Created:** [January 6, 2023, 12:20pm UTC](https://forum.playcanvas.com/t/solved-engine-only-start-stop-script-when-changing-a-scene/29170 "2023-01-06T12:20:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![iso74](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/i/ce7236/32.png) [@iso74](https://forum.playcanvas.com/u/iso74)\
**Post date:** [January 6, 2023, 12:20pm UTC](https://forum.playcanvas.com/t/solved-engine-only-start-stop-script-when-changing-a-scene/29170/1 "2023-01-06T12:20:16Z")

</div>

Hello,

I am using changeScene() to change a scene, each scene has different scripts running on an entity.  
I have the problem that the script continues running when the scene changes.  
How can I avoid this so that the script will restart each time I change the scene?

Thanks!

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [January 6, 2023, 1:09pm UTC](https://forum.playcanvas.com/t/solved-engine-only-start-stop-script-when-changing-a-scene/29170/2 "2023-01-06T13:09:19Z")

</div>

> [@iso74](#):
>
> I have the problem that the script continues running when the scene changes.

It shouldn’t if the entity it is attached to is destroyed. Is it event driven and if so, are you removing the event listeners when it is destroyed?

Can you replicate the issue in an Editor project?

---

<div class="post-metadata">

**Author:** ![iso74](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/i/ce7236/32.png) [@iso74](https://forum.playcanvas.com/u/iso74)\
**Post date:** [January 6, 2023, 1:29pm UTC](https://forum.playcanvas.com/t/solved-engine-only-start-stop-script-when-changing-a-scene/29170/3 "2023-01-06T13:29:08Z")

</div>

Honestly I am a bit lost here, issue coming from a tween.js script  
 ![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/3X/b/0/b03d53258d22424e0e80c43a5149c02a2919552c.png)

Have to figure out…

---

<div class="post-metadata">

**Author:** ![iso74](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/i/ce7236/32.png) [@iso74](https://forum.playcanvas.com/u/iso74)\
**Post date:** [January 6, 2023, 1:44pm UTC](https://forum.playcanvas.com/t/solved-engine-only-start-stop-script-when-changing-a-scene/29170/4 "2023-01-06T13:44:43Z")

</div>

Interesting is that its working when I use loadSceneHierarchy() instead of changeScene(), but I want changeScene() of course

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [January 6, 2023, 2:59pm UTC](https://forum.playcanvas.com/t/solved-engine-only-start-stop-script-when-changing-a-scene/29170/5 "2023-01-06T14:59:32Z")

</div>

If you can post the Editor project that showcases the issue, the community can help investigate

---

<div class="post-metadata">

**Author:** ![iso74](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/i/ce7236/32.png) [@iso74](https://forum.playcanvas.com/u/iso74)\
**Post date:** [January 6, 2023, 7:08pm UTC](https://forum.playcanvas.com/t/solved-engine-only-start-stop-script-when-changing-a-scene/29170/6 "2023-01-06T19:08:22Z")

</div>

It works now so far. It was some issue with the entity. Only thing now is that I cannot fully destroy the tweens / tween js. The tweens are kinda going crazy when I change the scene and switch back. As mentioned in the headline of this thread, I am using Engine only (and Vue3).

I do something like this in my Script in the initialize function:

```javascript
    for (i = 0; i < this.tweens.length; i++) {
      if (this.tweenInstances[i]) {
        this.tweenInstances[i].stop();
        this.tweenInstances[i] = undefined;
      }
 }

```

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [January 6, 2023, 7:26pm UTC](https://forum.playcanvas.com/t/solved-engine-only-start-stop-script-when-changing-a-scene/29170/7 "2023-01-06T19:26:28Z")

</div>

Yes, I asked if the issue could be replicated in the Editor as it makes it a lot easier for other people to help.

Right now, its incredibly difficult to help without a repro

---

<div class="post-metadata">

**Author:** ![iso74](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/i/ce7236/32.png) [@iso74](https://forum.playcanvas.com/u/iso74)\
**Post date:** [January 6, 2023, 7:36pm UTC](https://forum.playcanvas.com/t/solved-engine-only-start-stop-script-when-changing-a-scene/29170/8 "2023-01-06T19:36:10Z")

</div>

Understandable… In the meantime I solved it by using TWEEN\_JS.removeAll() bruteforce 🙂
