The problem is that you don’t disconnect the connection to SettingsFrame.TopBar.goBack.MouseButton1Click (the connection creation is copied below from your code).
Here’s an explanation of what happens with the current code. Connection for opening means a connection to Settings.MouseButton1Click. Connection for closing means a connection to SettingsFrame.TopBar.goBack.MouseButton1Click.
initial setUpMainMenu call (not found in the code you posted but I assume this is further down in the local script):
-connection for opening settings is created (opening connections: 1)
opening settings for the 1st time:
-opening connection is disconnected: (opening connections: 1-1 = 0),
-foo is printed once (total foos: 1),
-connection for closing is created (total closing connections: 1)
closing the settings for the 1st time:
-connection for opening settings is created 1 time (opening connections: 0+1)
opening the settings for the 2nd time:
-opening connection is disconnected: (opening connections: 1-1 = 0),
-foo is printed 1 time (total foos: 1+1 = 2),
-connection for closing is created (total closing connections: 1+1 = 2)
closing the settings for the second time:
-connection for opening settings is created 2 times because of 2 closing connections (opening connections: 0+2 = 2)
opening the settings for the 3rd time:
-opening connections are disconnected (opening connections: 2-2 = 0),
-foo is printed 2 times because of 2 opening connections (total foos: 2+2 = 4),
-connection for closing is created 2 times because of 2 opening connections (total closing connections: 2+2 = 4)
closing the settings for the 3rd time:
-connection for opening the settings is created 4 times because of 4 closing connections (opening connections: 0+4 = 4)
opening the settings for the 4th time:
-opening connections are disconnected (opening connections: 4-4 = 0),
-foo is printed 4 times because of 4 opening connections (total foos: 4+4 = 8),
-connection for closing is created 4 times because of 4 opening connections (total closing connections: 4+4 = 8)
closing the settings for the 4th time:
-connection for opening the settings is created 8 times because of 8 closing connections (opening connections: 0+8 = 8)
And so on…
In short, every time the settings are opened, all connections for opening settings are disconnected, as they should. However, because the connection for closing isn’t disconnected when the settings are closed, the closing connections stack up and thus the number of new opening connections created when closing the settings increases when the settings are opened and closed multiple times.