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 .
D&D 2024 has arrived! Pre-Order the new core rulebooks now.
Create a free account

sheetworker behavior question

Sheetworker #1 works like a champ. Click the button, get the info from the repeating section, send the info to the rolltemplate using custom roll parsing, and update the "armorusage" field in the repeating section. Sheettworker #2 almost works like I'd expect. it isn't in a repeating section, but otherwise does the same thing. It all appears to work EXCEPT the "usageshield" field isn't visually update. The actual attribute is update (verified if you click the button again as it shows that value or if you close and reload the sheet). Any idea why? relevant bits: <input type="text" name="attr_shield" placeholder="no shield"/> <input title = "Check if shield is metal" type="checkbox" value=1 name="attr_metal"> <input type="number" name="attr_shieldacp" placeholder="0" /> <input  type="text"  name="attr_usageshield" > <button type="action" name="act_usagecheckshield">Use</button> <fieldset class="repeating_armor"> <input type="text" name="attr_armorgear"> <input type="text" name="attr_armoracp"> <select name="attr_armorusage"><option value="na">na</option><option value="dep">dep</option><option value="d4">d4</option><option value="d6">d6</option><option value="d8">d8</option><option value="d10">d10</option><option value="d12">d12</option></select> <button type="action" name="act_usagepro">Use</button> </fieldset> Sheet worker 1 (works) on('clicked:repeating_armor:usagepro', (eventInfo) => {   getAttrs(["repeating_armor_armorgear","repeating_armor_armorusage","repeating_armor_armordep"], function(values) {     const trigger = eventInfo.sourceAttribute.split('_');     const rowid = trigger[2];     var gear = values.repeating_armor_armorgear;     var die  = values.repeating_armor_armorusage;     var val  = die.slice(1);     var dep  = values.repeating_armor_armordep;     var roll = (Math.floor(Math.random() * val))+1;     if (roll > 2){       var result = 1;       var newdie = val;     }else{       var result = 0       var newdie = (val -2);       if(val == 12){var red = "d10"}       if(val == 10){var red = "d8"}       if(val == 8){var red = "d6"}       if(val == 6){var red = "d4"}       if(val == 4){var red = "dep"}       setAttrs({ ['repeating_armor_'+rowid+'_armorusage']: red,       });     }     var roll_text = `&{template:BXDH_Usage}{{name=@{character_name}}}{{action=USAGE CHECK}}{{detail=for ${gear}}}{{basedie=${die}}}{{result=[[${result}]]}}{{newval=[[${newdie}]]}}{{newdie=d${newdie}}}`;     startRoll(roll_text, (results) => {       finishRoll(results.rollId, {});     }); }); }); Sheet worker 2 (has problems) on('clicked:usagecheckshield', () => {   getAttrs(["shield","metal","usageshield"], function(values) {     var gear = values.shield;     var meta  = values.metal;     var die  = values.usageshield;     var val  = die.slice(1);     var roll = (Math.floor(Math.random() * val)) + 1;     if (roll > 2){       var result = 1;       var newdie = val;     }else{       var result = 0;       var newdie = (val -2);       if(val == 12){var red = "d10"}       if(val == 10){var red = "d8"}       if(val == 8) {var red = "d6"}       if(val == 6) {var red = "d4"}       if(val == 4) {var red = "dep"}       setAttrs({ usageshield: red       });     }     var roll_text = `&{template:BXDH_Usage}{{name=@{character_name}}}{{action=SHIELD USAGE CHECK}}{{detail=for ${gear}}}{{basedie=${die}}}{{result=[[${result}]]}}{{newval=[[${newdie}]]}}{{newdie=d${newdie}}}{{roll=[[${roll}+${meta}]]}}`;     startRoll(roll_text, (results) => {       finishRoll(results.rollId, {});     });   }); }); I'm sure there are numerous code improvements that could be made, but I'm really curious why worker 1 one updates an attribute while worker 2 doesn't (and how to address it).
Sheet Author
Hi Duck, I'm a neophyte js user at best so I'm not sure if it matters but, I copied the 2nd worker into my editor and my linter complained heavily... so I made a few adjustments.  Used const for vairables that do not change.  Moved the variable declarations outside the if()else{}.  Changed the equality checks to "===".  Similar changes could be made to the 1st worker as well, but if it ain't broke... ;-P  Again, not sure if it matters, but I assign values to all my attributes even if it's value=''.  I also noticed the attr_metal input has value=1 without quotes.  Not sure if this can trip up the rendered html since it should have quotes.  I would try fixing those and see if this worker makes any difference. on('clicked:usagecheckshield', () => { getAttrs(['shield', 'metal', 'usageshield'], function (values) { const gear = values.shield; const meta = values.metal; const die = values.usageshield; const val = die.slice(1); const roll = Math.floor(Math.random() * val) + 1; let result = ''; let newdie = ''; let red = ''; if (roll > 2) { result = 1; newdie = val; } else { result = 0; newdie = val - 2; if (val === 12) { red = 'd10'; } if (val === 10) { red = 'd8'; } if (val === 8) { red = 'd6'; } if (val === 6) { red = 'd4'; } if (val === 4) { red = 'dep'; } setAttrs({ usageshield: red, }); } const roll_text = `&{template:BXDH_Usage}{{name=@{character_name}}}{{action=SHIELD USAGE CHECK}}{{detail=for ${gear}}}{{basedie=${die}}}{{result=[[${result}]]}}{{newval=[[${newdie}]]}}{{newdie=d${newdie}}}{{roll=[[${roll}+${meta}]]}}`; startRoll(roll_text, (results) => { finishRoll(results.rollId, {}); }); }); });

Edited 1700542296
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Yep, the issue was the variable declarations inside the ifs. Ifs are code blocks and so act as a block scope for code which means anything new declared in them isn't accessible outside of them. I'll note that this shouldn't work in sheetworker #1 either, so I'd recommend checking that that is actually doing what you think it is.
Thanks!  I'll polish these up a bit and report back.

Edited 1700575102
the only change i made was die is a sliced string, so changed === 12 to ==='12' (etc.) and everything worked like a charm after defining the variables correctly. thank you both!