Game logic only working after updating preview

How do I…

The logic of the game is working perfectly, the only caveat is that i need to press update preview to get it to work properly. How do I get the game logic to work from the first load without needing to press update preview to hot reload the game to get the array values right.

What is the expected result

Each element has an ID, and when the elements are bonded together, the ID of the opposing element gets placed in the elements array. For example, elementA has an id of 1 and elementB has an id of 2, so the array of A would read [2] and the array of B would read [1]. This works fine. When an element is bonding to numerous elements (for example, both A and B) the array should read [1,2].

What is the actual result

When the preview is first launched. The array of an element that is bonded to numerous would read [1,2,1]. After the update preview button is pressed, it then reverts back to [1,2].

Related screenshots

This is the code.

Results from first run:

After update preview:

Please, I would appreciate any help on this issue. Thank you