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 .
×
What’s a beholder’s favorite food? Eyes cream. 👀🍦

Repeating section sheetworker troubles

1608637251
Oboe
Sheet Author
Hey, so I am having a problem with updating attributes based on different transformations the players can do. To achieve the transformations, I have created a repeating section which allows them to input the updates to their attributes based on the transformations. However, the script does not seem to be working. I do edit the attributes elsewhere and that works fine, but it does here for some reason and I can't figure out why, any help would be appreciated! Code for the repeating section is posted below: < fieldset   class = "repeating_transformations-table" >                  < div   class = "transformations-table" >                      < div   class = "tname" >                          < input   type = "text"   name = "attr_transformation_name"   style = " width: 80px; " >                          < input   type = "checkbox"   name = "attr_transformation_on"   value = "1" >                      </ div >                      < div   class = "tagi" >< input   type = "number"   name = "attr_transformation_agi"   value = "0" ></ div >                      < div   class = "tins" >< input   type = "number"   name = "attr_transformation_ins"   value = "0" ></ div >                      < div   class = "tper" >< input   type = "number"   name = "attr_transformation_per"   value = "0" ></ div >                      < div   class = "tpot" >< input   type = "number"   name = "attr_transformation_pot"   value = "0" ></ div >                      < div   class = "tsch" >< input   type = "number"   name = "attr_transformation_sch"   value = "0" ></ div >                      < div   class = "tspi" >< input   type = "number"   name = "attr_transformation_spi"   value = "0" ></ div >                      < div   class = "tstr" >< input   type = "number"   name = "attr_transformation_str"   value = "0" ></ div >                      < div   class = "tten" >< input   type = "number"   name = "attr_transformation_ten"   value = "0" ></ div >                  </ div >                  < script   type = "text/worker" >                     on("change:repeating_transformations-table", function(){                         getAttrs(["repeating_transformations-table_TRANSFORMATION_ON",  "repeating_transformations-table_TRANSFORMATION_AGI",                           "repeating_transformations-table_TRANSFORMATION_INS",                           "repeating_transformations-table_TRANSFORMATION_PER",                           "repeating_transformations-table_TRANSFORMATION_POT",                           "repeating_transformations-table_TRANSFORMATION_SCH",                           "repeating_transformations-table_TRANSFORMATION_SPI",                           "repeating_transformations-table_TRANSFORMATION_STR",                           "repeating_transformations-table_TRANSFORMATION_TEN",                           "AGILITY", "INSIGHT", "PERSONALITY", "POTENCY", "SCHOLARSHIP",                           "SPIRIT", "STRENGTH", "TENACITY"], function(values){                             var on = values.repeating_transformations-table_TRANSFORMATION_ON;                                                          var agi = values.repeating_transformations-table_TRANSFORMATION_AGI;                             var hagi = values.AGILITY;                                                          var ins = values.repeating_transformations-table_TRANSFORMATION_INS;                             var hins = values.INSIGHT;                                                          var per = values.repeating_transformations-table_TRANSFORMATION_PER;                             var hper = values.PERSONALITY;                                                          var pot = values.repeating_transformations-table_TRANSFORMATION_POT;                             var hpot = values.POTENCY;                                                          var sch = values.repeating_transformations-table_TRANSFORMATION_SCH;                             var hsch = value.SCHOLARSHIP;                                                          var spi = values.repeating_transformations-table_TRANSFORMATION_SPI;                             var hspi = values.SPIRIT;                                                          var str = values.repeating_transformations-table_TRANSFORMATION_STR;                             var hstr = values.STRENGTH;                                                          var ten = values.repeating_transformations-table_TRANSFORMATION_TEN;                             var hten = values.TENACITY                                                          if(on==0){                                 hagi = hagi-agi;                                 setAttrs({                                     "agility": hagi                                 });                                 hins = hins-ins;                                 setAttrs({                                     "insight": hins                                 });                                                                  hper = hper-per;                                 setAttrs({                                     "personality":hper                                 });                                 hpot = hpot-pot;                                 setAttrs({                                     "potency":hpot                                 });                                 hsch = hsch-sch;                                 setAttrs({                                     "scholarship":hsch                                 });                                 hspi = hspi-spi;                                 setAttrs({                                     "spirit":hspi                                 });                                 hstr = hstr-str;                                 setAttrs({                                     "strength":hstr                                 });                                 hten = hten-ten;                                 setAttrs({                                     "tenacity":hten                                 });                             }else if(on == 1){                                 hagi = hagi+agi;                                 setAttrs({                                     "agility": hagi                                 });                                 hins = hins+ins;                                 setAttrs({                                     "insight": hins                                 });                                                                  hper = hper+per;                                 setAttrs({                                     "personality":hper                                 });                                 hpot = hpot+pot;                                 setAttrs({                                     "potency":hpot                                 });                                 hsch = hsch+sch;                                 setAttrs({                                     "scholarship":hsch                                 });                                 hspi = hspi+spi;                                 setAttrs({                                     "spirit":hspi                                 });                                 hstr = hstr+str;                                 setAttrs({                                     "strength":hstr                                 });                                 hten = hten + ten;                                 setAttrs({                                     "tenacity":hten                                 });                             }                         });                     });                  </ script >              </ fieldset >
1608649254

Edited 1608649313
Andreas J.
Forum Champion
Sheet Author
Translator
You should have your script/sheetworker codeblock stand by itself on the character sheet, and not inside a repeating section. I'm 95% sure you having it embedded in there is the reason it doesn't work. It have never occurred to me to place it there, nor have I ever seen it done before. Most sheet creators place the single sheetworker element at the bottom of the sheet so it's easy to find, and always in the same place. This is also what you should see in every sheet example you've ever seen, but now thinking about it, don't think it's explicitly stated as an recommendation.
1608651025
Oboe
Sheet Author
Ah, I didn't know that. I have tried it, but it still doesn't seem to want to work. I know using a script worker in repeating section has to be handled differently to how a sheet worker normally works.
1608652656

Edited 1608652866
Andreas J.
Forum Champion
Sheet Author
Translator
Oh, and looking at the actual code, I see you should make as few SetAttr() sections as possible, Roll20 is extremely slow on it so having 5 separate SetAttr() get triggered after each other might take seconds to finish updating everything. I'm not great at them either, but look up more of the examples and other pages on the stuff you can find here: <a href="https://wiki.roll20.net/Sheet_Worker_Scripts" rel="nofollow">https://wiki.roll20.net/Sheet_Worker_Scripts</a> SettAttr for repeating sections are not defined like you done there, that's for sure. I suggest you ad some console.log() instances to the sheetworker and see if your values are even saved to the variables(I think that's incorrectly defined as well), like when you have var agi = values.repeating_transformations-table_TRANSFORMATION_AGI; console.log(agi);
1608680691

Edited 1608875611
Oosh
Sheet Author
API Scripter
I vaguely remember there being some issues with hyphens in repeating section names... or is that no longer an issue?? But it might be worth considering repeating_transformationsTable or repeating_transformations_table. Repeating row IDs start with a dash, so if a script does a regex search for /_(-[^_]*)_/ for example, to try to grab the repeating row ID, that hyphen will trip it up as it comes before the row ID. May not be an issue though, not sure... probably good practice to avoid it though, as it's used in unique ID's and math. As Andreas said above, try to minimise the use of getAttrs and setAttrs. The usual way is to push all of your sheetworker changes to an output object, then run setAttrs once with the object as the input. A quick example (this is all inside the getAttrs callback): let on = parseInt(values.repeating_transformations-table_TRANSFORMATION_ON); let agi = parseInt(values.repeating_transformations-table_TRANSFORMATION_AGI); let hagi = parseInt(values.AGILITY); let ins = parseInt(values.repeating_transformations-table_TRANSFORMATION_INS); let hins = parseInt(values.INSIGHT); let per = parseInt(values.repeating_transformations-table_TRANSFORMATION_PER); let hper = parseInt(values.PERSONALITY); let output = {} if (on === 0) { output.agility = hagi-agi; output.insight = hins-ins; output.personality = hper-per; } setAttrs(output); Two other things in there: - var is an older way to define variables in JS - it used to be the only way. Somewhere on these boards there's a pretty good post from Aaron, full of scorn and invective and concluding that the introduction of let and const should allow var to go and die the lonely death it deserves, off in some forgotten corner (or something to that effect). edit - this isn't the post I was thinking of, but still covers the subject. You could also use const here if you wish, I default to let because less keyboard - you'll almost always want to run parseInt() on anything you grab from a Roll20 sheet, as it prefers to store things as strings. You can get integers stored in there (some compendium data comes in as actual integers) but as soon as it's edited it'll be a string. Safest to just parseInt (or parseFloat) on anything you need for math. edit 2 - there are other ways to enter key:value pairs into an object as well. This is a more objecty-looking syntax which achieves the same thing: let output = {} if (on === 0) { output = { agility: hagi-agi, insight: hins-ins, personality: hper-per, } setAttrs(output);
1608799484
GiGs
Pro
Sheet Author
API Scripter
I see two issues. It's already been mentioned, but you shouldnt have the script block inside the fieldset like that. I think you've mode it since then. The more immediate problem is the hyphen in your repeating section name. It's causing all your variable assignments to fail. For example this line: var&nbsp;on&nbsp;=&nbsp;values.repeating_transformations-table_TRANSFORMATION_ON; The hyphen there is being interpreted as a minus sign, so the script processor is trying to get the value of an attribute name repeating_transformations &nbsp;(which doesnt exist) and then trying to subtract from that the value of a variable named&nbsp; table_TRANSFORMATION_ON , which also doesnt exist. If you have a hyphen in an attribute name, you have to use the alternate syntax: var&nbsp;on&nbsp;=&nbsp;values['repeating_transformations-table_TRANSFORMATION_ON']; Fix all your variable assignments in this format. Alternatively you can change the repeating section name - if you do that, don't &nbsp;add a second underscore, and dont use capital letters. Dont do either of these: repeating_transformationsTable repeating_transformations_table Either of these is fine: repeating_transformationstable repeating_transformations But with the syntax above, you can keep the hyphen. You have another problem with lines like this: hagi&nbsp;=&nbsp;hagi-agi; All attribute values are stored as strings, so this probably wont work properly (at leats, not reliably). You need to coerce the attribute values to make sure they are numbers. You can do that when assigning the values, like: var&nbsp;on&nbsp;=&nbsp;+values['repeating_transformations-table_TRANSFORMATION_ON'] || 0; Andreas is correct to point out the setAttrs issues. You can do that (it wont stop it working), it's just very inefficient. It's much better to create a variable to hold all the attributes, then do a single setAttrs at the end of the worker. You can do that like this: on ( 'change:repeating_transformations-table' ,&nbsp; function (){ &nbsp;&nbsp;&nbsp;&nbsp; getAttrs ([ 'repeating_transformations-table_TRANSFORMATION_ON' ,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'repeating_transformations-table_TRANSFORMATION_AGI' ,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'repeating_transformations-table_TRANSFORMATION_INS' ,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'repeating_transformations-table_TRANSFORMATION_PER' ,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'repeating_transformations-table_TRANSFORMATION_POT' ,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'repeating_transformations-table_TRANSFORMATION_SCH' ,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'repeating_transformations-table_TRANSFORMATION_SPI' ,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'repeating_transformations-table_TRANSFORMATION_STR' ,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'repeating_transformations-table_TRANSFORMATION_TEN' ,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'AGILITY' ,&nbsp; 'INSIGHT' ,&nbsp; 'PERSONALITY' ,&nbsp; 'POTENCY' ,&nbsp; 'SCHOLARSHIP' ,&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'SPIRIT' ,&nbsp; 'STRENGTH' ,&nbsp; 'TENACITY' ],&nbsp; function ( values ){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; on &nbsp;=&nbsp;+ values [ 'repeating_transformations-table_TRANSFORMATION_ON' ]&nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; agi &nbsp;=&nbsp;+ values [ 'repeating_transformations-table_TRANSFORMATION_AGI' ]&nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; hagi &nbsp;=&nbsp;+ values . AGILITY &nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; ins &nbsp;=&nbsp;+ values [ 'repeating_transformations-table_TRANSFORMATION_INS' ]&nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; hins &nbsp;=&nbsp;+ values . INSIGHT &nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; per &nbsp;=&nbsp;+ values [ 'repeating_transformations-table_TRANSFORMATION_PER' ]&nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; hper &nbsp;=&nbsp;+ values . PERSONALITY &nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; pot &nbsp;=&nbsp;+ values [ 'repeating_transformations-table_TRANSFORMATION_POT' ]&nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; hpot &nbsp;=&nbsp;+ values . POTENCY &nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; sch &nbsp;=&nbsp;+ values [ 'repeating_transformations-table_TRANSFORMATION_SCH' ]&nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; hsch &nbsp;=&nbsp;+ values . SCHOLARSHIP &nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; spi &nbsp;=&nbsp;+ values [ 'repeating_transformations-table_TRANSFORMATION_SPI' ]&nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; hspi &nbsp;=&nbsp;+ values . SPIRIT &nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; str &nbsp;=&nbsp;+ values [ 'repeating_transformations-table_TRANSFORMATION_STR' ]&nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; hstr &nbsp;=&nbsp;+ values . STRENGTH &nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; ten &nbsp;=&nbsp;+ values [ 'repeating_transformations-table_TRANSFORMATION_TEN' ]&nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var &nbsp; hten &nbsp;=&nbsp;+ values . TENACITY &nbsp;||&nbsp; 0 ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const &nbsp; output &nbsp;=&nbsp;{}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const &nbsp; onx &nbsp;=&nbsp;( on &nbsp;?&nbsp; 1 :&nbsp;- 1 ); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output . agility &nbsp;=&nbsp; hagi &nbsp;+&nbsp; onx &nbsp;*&nbsp; agi ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output . insight &nbsp;=&nbsp; hins &nbsp;+&nbsp; onx &nbsp;*&nbsp; ins ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output . personality &nbsp;=&nbsp; hper +&nbsp; onx &nbsp;*&nbsp; per ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output . potency &nbsp;=&nbsp; hpot &nbsp;+&nbsp; onx &nbsp;*&nbsp; pot ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output . scholarship &nbsp;=&nbsp; hsch &nbsp;+&nbsp; onx &nbsp;*&nbsp; sch ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output . spirit =&nbsp; hspi &nbsp;+&nbsp; onx &nbsp;*&nbsp; spi ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output . strength &nbsp;=&nbsp; hstr &nbsp;+&nbsp; onx &nbsp;*&nbsp; str ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output . tenacity &nbsp;=&nbsp; hten &nbsp;+&nbsp; onx &nbsp;*&nbsp; ten ; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setAttrs ( output ); &nbsp;&nbsp;&nbsp;&nbsp;}); }); The output &nbsp;variable holds the various attribute values. You can see how they are each being assigned to it. Your if statement can be optimised. You have essentially: if ON, attribute = something PLUS something,&nbsp; else attribute = something MINUS something,&nbsp; The onx attribute provides a handy way to remove the if statement. It's adding when ON = 1, and subtracting when ON = 0.