The $ is a builder-specific thing and seems Python-specific as well. See here: Referencing conditions file in code component
In my experience, it doesn’t work well if the variable being referenced is defined in a code element rather than a condition file. I’m not entirely sure why. Online, it also has a tendency to cause errors like this one, though again, I could not give you a complete explanation why: Reference error "$ not defined" on Pavlovia