I'm try to implement this solution to my document. On TexStudio, it works well however on Overleaf it doesn't compile. It complains about inconsistent { and } after tracing my code I noticed one of my { is being ignored see picture below.
In line 89, the { after \pgfkeysvalueof is being ignored and causing compilation errors. The same applies in line 90.

\begin{scope}and\end{scope}are actually matched properly based on the usage of the TikZ keys. You can disable code check for portions of code that are problematic: https://www.overleaf.com/learn/how-to/Code_Check#Disabling_Code_Check_for_part_of_a_file – Paul Gessler Sep 09 '20 at 18:10