Solution
If you are using global variables with references, the mechanical actions "Switch Until Released" and "Latch Until Released" will not work properly. Instead of it use the other 4 mechanical actions of the button ("Switch When Pressed", "Switch When Released", "Latch When Pressed", "Latch When Released").
Right-Click on the button, go to the Mechanical Action section and select one of the listed above.