Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Script Not Working Suddenly

For one of my campaigns, the API scripts are suddenly not working.  I am getting some error that starts off with Cannot Read Property 'name' of undefined.  The same scripts work in another campaign I just created using the same purchased TOA module.  Any thoughts?
1592359523
timmaugh
Forum Champion
API Scripter
Just a suggestion until the big guns get here, but that can happen if strict mode is used in a script. The error should tell you which function and script is failing. Try isolating that script by disabling it and running others. Then run it in isolation (disabling others). The scripts all get rolled up into one long file, so I would wonder if one appearing before the problematic one is setting the use strict parameter. (I say that as a question to those more knowledgeable.) I'll test that out...
Because roll20 runs all the scripts together as one big program, the failure to end a script with a semi-colon can mess up what follows.  You can troubleshoot by disabling half of your scripts at a time to find the one causing the problem. Then add a semicolon on its own line at the beginning and at the end.  If that works, you are good to go.  If not, the specific script may need some attention.
I deleted and re-added the scripts.  They work on my other campaigns, but not one in particular.
1592415612
The Aaron
Roll20 Production Team
API Scripter
Cannot Read Property 'name' of undefined Occurs when you have a variable in Javascript that you expect to contain an object and you try to reference a property of that object (name in this case) without checking if the variable is valid.  It happens most often in Roll20 scripts when the author gets a Roll20 Object with findObjs() or getObj() but doesn't check the result before using it. let brokenObj = findObjs({type:'graphic', name: 'something that does not exist'}); log(brokenObj.name); If you can list the scripts you have, we can probably figure out which one has the problem.  If you know what you're doing when it happens (moving a token, restarting the sandbox, making a roll in chat) that can help narrow it down.
1592417563

Edited 1592417672
Ada L.
Marketplace Creator
Sheet Author
API Scripter
timmaugh said: Just a suggestion until the big guns get here, but that can happen if strict mode is used in a script.  ...and all API scripts really  should use strict mode. It helps to identify errors and code smells so that they're fixed earlier rather than later. Using Aaron's example, a way to work around this error case could be something like this: let brokenObj = findObjs({type:'graphic', name: 'something that does not exist'})[0]; if (brokenObj) { // Make sure that the object resolves to a truthy value. i.e. not undefined.     // We have our object. Yay!     doSomethingWith(brokenObj.name); } else {     // We don't have our object.     log(`Oops! Good thing we checked that brokenObj exists first before trying to access brokenObj.name!`);     doSomethingElse(); }
1592417942
The Aaron
Roll20 Production Team
API Scripter
Woot, agree. Note: using ES6+ Fat Arrows gets you strict mode by default: on('ready',()=>{ // it's strict in here! });
1592418585

Edited 1592418778
I can't get the beyond importer to work.  But just for one of my campaigns.  When I paste it into other campaigns no issues.  I also have the 5th edition ogl companion loaded.