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 .
×

Needing a hand with sheetworkers

I went through the sheetworker tutorial and can't get it to work.  I'm sure I'm missing something really simple.  I just tried to have it calculate an attribute total but when I run it stays at zero no matter what I set the stats at. < button class = "txt-btn attr-btn str-btn" name = "roll_strength" value = "/roll 2d10 + @{strength}" type = "roll" > Strength </ button > < input style = " background:none;border:none;font-weight:bold;font-size:18px " type = "number" class = "stat-input" name = "attr_strength" title = "strength total" readonly /> < input type = "number" class = "stat-input" name = "attr_strength_temp" value = "" title = "strength temp" /> < input type = "number" class = "stat-input" name = "attr_strength_gear" value = "" title = "strength gear" /> < input type = "number" class = "stat-input" name = "attr_strength_trait" value = "" title = "strength trait" /> < input type = "number" class = "stat-input" name = "attr_strength_other" value = "" title = "strength other" /> < input type = "number" class = "stat-input" name = "attr_strength_rank" value = "" title = "strength rank" /> < script >     on ( "change:strength_temp change:strength_gear change:strength_trait change:strength_other change:strength_rank sheet:opened" , function () {         getAttrs ([ "strength_temp" , "strength_gear" , "strength_trait" , "strength_other" , "strength_rank" ], function ( values ) {             let strength_temp = parseInt ( values . strength_temp )|| 0 ;             let strength_gear = parseInt ( values . strength_gear )|| 0 ;             let strength_trait = parseInt ( values . strength_trait )|| 0 ;             let strength_other = parseInt ( values . strength_other )|| 0 ;             let strength_rank = parseInt ( values . strength_rank )|| 0 ;             let str = strength_temp + strength_gear + strength_trait + strength_other + strength_rank ;             setAttrs ({                                              strength: str                         });         });     }); </ script >
1644948025
Kraynic
Pro
Sheet Author
First, you don't have the correct label on your sheetworker section.&nbsp; <a href="https://wiki.roll20.net/Building_Character_Sheets#JavaScript_2" rel="nofollow">https://wiki.roll20.net/Building_Character_Sheets#JavaScript_2</a> &lt;script type="text/worker"&gt; ---sheetworkers here--- &lt;/script&gt; I also don't see an input with an attribute of strength.&nbsp; Usually it would either be hidden or would be readonly.&nbsp; Have you created the attribute that the sheetworker is setting?&nbsp;
1644949211

Edited 1644949325
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
You have to specify what type of script the script block is. Sheetworkers have to be in a "text/worker" script block. This looks like so (change in bold): &lt;script type="text/worker" &gt; on("change:strength_temp change:strength_gear change:strength_trait change:strength_other change:strength_rank sheet:opened", function() { getAttrs(["strength_temp","strength_gear","strength_trait","strength_other","strength_rank"], function(values) { let strength_temp = parseInt(values.strength_temp)||0; let strength_gear = parseInt(values.strength_gear)||0; let strength_trait = parseInt(values.strength_trait)||0; let strength_other = parseInt(values.strength_other)||0; let strength_rank = parseInt(values.strength_rank)||0; let str = strength_temp + strength_gear + strength_trait + strength_other + strength_rank; setAttrs({ strength: str }); }); }); &lt;/script&gt; EDIT: Sniped by Kraynic! Although I do see the strength attribute right under the button. Also, since it looks like you're diving into sheet creation, you may be interested in following my series of weekly tutorial posts on sheet creation; A Sheet Author's Journey
1644949916
Kraynic
Pro
Sheet Author
Scott C. said: EDIT: Sniped by Kraynic! Although I do see the strength attribute right under the button. Oh yes, totally missed that it was there.
1644950920

Edited 1644950996
John D.
Sheet Author
Hello Mr. Lair! I believe your syntax is a bit off in a few places.&nbsp; I reconstructed your HTML and JavaScript, tested and worked: &lt;button class="txt-btn attr-btn str-btn" name="roll_strength" value="&amp;{template:default} {{roll=[[2d10+@{strength}]]}}" type="roll"&gt;Strength&lt;/button&gt; &lt;input type="number" class="stat-input" name="attr_strength" value=0 title="strength total" readonly /&gt; &lt;input type="number" class="stat-input" name="attr_strength_temp" value=0 title="strength temp"/&gt; &lt;input type="number" class="stat-input" name="attr_strength_gear" value=0 title="strength gear"/&gt; &lt;input type="number" class="stat-input" name="attr_strength_trait" value=0 title="strength trait"/&gt; &lt;input type="number" class="stat-input" name="attr_strength_other" value=0 title="strength other"/&gt; &lt;input type="number" class="stat-input" name="attr_strength_rank" value=0 title="strength rank"/&gt; &lt;rolltemplate class="sheet-default"&gt; &lt;div&gt;My roll was {{roll}}&lt;/div&gt; &lt;/rolltemplate&gt; &lt;script type="text/worker"&gt; const strength_array = ['strength_temp','strength_gear','strength_trait','strength_other','strength_rank']; const strength_events = `change:${strength_array.join(' change:')}`; on(strength_events, () =&gt; { getAttrs(strength_array, values =&gt; { const setObj = {}; const strength_values = _.chain(strength_array) .map(s =&gt; { return parseInt(values[`${s}`]) || 0; }) .reduce((memo, num)=&gt;{ return memo + num; }, 0) .value(); setObj.strength = strength_values; setAttrs(setObj,{silent:true}); }); }); &lt;/script&gt; The main issues I found were: &lt;script&gt; should have included&nbsp;&lt;script type="text/worker"&gt;, without declaring the type the script will never launch! In the value string of your roll button you need to define which roll template the roll should use to output to the chat window.&nbsp; I used "default", which is built-in, but you can supply the name of the roll template you want the roll to appear on.&nbsp; Build your own roll templates&nbsp;using HTML/CSS as in the example above. You need to define in the value &nbsp;string of your roll button a&nbsp;"roll" variable, which will be displayed in the roll template by referencing it as {{roll}}.&nbsp; Also, encapsulate the roll string in double square parentheses to tell chat this is a function and not text to be displayed as-is. As for your sheet worker...you probably don't need to run this ever time the sheet opens.&nbsp; A starting character will have all values set to 0 until altered, and then you can calculate everything.&nbsp; This could get process heavy for opening a sheet for zero value. I also changed all your inputs to int from strings because they are number inputs. All your "let" declarations should be "const" since you're not changing the value of the variable, this will run leaner in memory.&nbsp; Objects are an odd exception to const, you just need to define them and then you can manipulate the data they contain. Happy to answer any questions you might have but I got to get back to work.&nbsp; This was fun!&nbsp; Hope it helps.&nbsp; ;)
1644951236
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
John's got some great critique, I will note one fillip of sheet design on Roll20 though. All attributes should be assumed to come in as strings, regardless of what type they are because when the input is edited by a user, it will always be a string.
DOH!&nbsp; Scott, thanks for pointing that out.&nbsp; Seems this has always been on the periphery of my mind but never came out of the fog, always needing to use parseInt/parseFloat when pulling numerical values from getAttrs.&nbsp; It all makes sense now!
1644953035

Edited 1644954943
Whoops, guess I missed that script label thing in the tutorial.&nbsp; A big thank you to everyone, you're all very helpful, and FAST.&nbsp; :) Scott C. I read your tutorial posts a few days ago.&nbsp; My biggest problem right now is just that my brain is overflowing.&nbsp; I'm trying to learn too much at once.&nbsp; I'm not super familiar with all of the terminology yet so many of the tutorials I am looking at barely get absorbed.&nbsp; Perfect example:&nbsp; John D. said he changed inputs from int to strings... what the heck is an int or string?&nbsp; lol.
You are most welcome, Mr. Liar! I totally understand where you're coming from.&nbsp; It's a lot to take in but just keep coding, reading, trying new things, posting here.&nbsp; :) 'int' is shorthand for integer , a number with no fractional parts.&nbsp; A 'string' is data type used to represent text instead of a number (like int).&nbsp; You can perform mathematical function on an int, but not on a string.&nbsp; Strings are usually what you want to use when presenting something to a player, like, "Tim The Liar hacks John with his longsword!"&nbsp; An int can be used with a string but you must convert it, such as if you wanted to use the following string in chat, "Tim The Liar hacks John for 10 damage with his longsword!"&nbsp; And like Scott pointed out, all values are saved to a sheet as text, so if you want to use a value in mathematical functions, you need to convert it (i.e. parseInt()) but it seems you got that part down.&nbsp; ;)
Okay that makes sense. Thank you.&nbsp; Honestly, the only part I have down is copying what I find here or in the wiki and tweaking it.&nbsp; But I've been doing as you said. Coding, trying new things, reading, posting.&nbsp; Definitely making progress.&nbsp; I'm sure you'll see more posts from me in the near future.&nbsp; ;)&nbsp;