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

Learning how to buttons with scriptworkers

1596791513
Richard T.
Pro
Marketplace Creator
Sheet Author
Compendium Curator
I need to just get down and learn javascript because I always feel like an idiot when I can't do simple things, but for now I have you smart people.  So, I'm trying to do a simple button simple that lets people increment an attribute, by +1 or +3.  I have this button: <button type="action" name="act_accuracy+3">+3</button> With this script  <script type="text/worker"> on('clicked:accuracy+3', function() { getAttrs(['accuracy-current'], function(values) { // get the level stat, and coerce into a numerical value. const level = +values.accuracy-current || 0; // increase by 1 const newlevel = level +3; // save the updated attribute to the sheet setAttrs({ accuracy-current: newlevel }); }); }); </script> I practically copy and pasted it from the wiki and it doesn't wooorrk. 
1596805129

Edited 1596805460
GiGs
Pro
Sheet Author
API Scripter
Your button name might be an issue- it's best to avoid non-alphanumeric characters - that + in the name might be a problem. But it might be okay.  What wont work is the hyphen in your attribute name being set. You need to enclose names like that in quotes, like         setAttrs({ 'accuracy-current': newlevel }); Likewise, earlier in the function, you cant use this syntax const level = +values.accuracy-current || 0; with names containing hyphens (or other non-alphanumerical characters). You need to use the alternative syntax: const level = +values['accuracy-current'] || 0; This is one reason it is better to use underscores than dashes or hyphens. underscores are one character it is safe to use in both syntaxes, and no need for quotes in setAttrs.
1596838018
Richard T.
Pro
Marketplace Creator
Sheet Author
Compendium Curator
Thanks for the response, GiGs. I did your suggested changes and it's still being a bug.  I've renamed the pertinent action buttons like so: <button type="action" name="act_accuracyplus3">+3</button> and the script now looks like this: on('clicked:accuracyplus3', function() {     getAttrs(['accuracy-current'], function(values) {         // get the level stat, and coerce into a numerical value.         const level = +values.['accuracy-current'] || 0;          // increase by 1         const newlevel = level +3;         // save the updated attribute to the sheet         setAttrs({            'accuracy-current': newlevel          });    }); });
1596838524
GiGs
Pro
Sheet Author
API Scripter
This line shouldnt have a dot in it:   const level = +values.['accuracy-current'] || 0; it should be   const level = +values['accuracy-current'] || 0;
1596838816
Richard T.
Pro
Marketplace Creator
Sheet Author
Compendium Curator
Whoo! GiGs wheres your patreon/payal!? 
1596838936
GiGs
Pro
Sheet Author
API Scripter
You could of course rename the attribute  accuracy-current to  accuracy_current and avoid this issue entirely. I always recommend to people new to javascript to use only letters and underscores in attribute names for this reason. (You can use numbers, but not at the start of the attribute name.)
1596839076
GiGs
Pro
Sheet Author
API Scripter
Richard T. said: Whoo! GiGs wheres your patreon/payal!?  Hehe. I dont think we're allowed to promote ourself on the forums here.
1596839393
Richard T.
Pro
Marketplace Creator
Sheet Author
Compendium Curator
Whats a little passing of funds between colleagues? 
1596843366
GiGs
Pro
Sheet Author
API Scripter
Thank you - much appreciated :)