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.

Hello,  So im trying to set up a script that will keep my number from going below 0 but i dont seem to understand how to do it. im trying to keep the Combat_Focus_Sanity_Total from becoming 0 even if the Combat_Focus_Sanity value goes below 0. Im sure my lack of understanding id biting me here. if someone could help me here and tell me what im doing wrong, i would appreciate it. Ill post the HTML below. <div class="Powers"> <h1>Simple Powers</h1> <input  type="hidden"   name="attr_Powers_Sanity_Reduction"  value="[[(@{PsychicGift_Checkbox}+@{PsychicTraining_Checkbox}+@{AdvancePsychicTraining_Checkbox})]]"         disabled="true"/> <div class="Heading"> <h4>&nbsp </h4> <h4>&nbsp </h4> <h4>Power</h4> <h4>Cast</h4> <h4>Range</h4> <h4>Sanity</h4> <h4>Duration</h4> <h4>Damage</h4> <h4>Save</h4> <h4>Type</h4> <h4>Effect</h4> </div> <div class="CombatFocus"> <input  type="checkbox" name="attr_Combat_Focus"  value="1"> <button type="roll" value="&{template:default} {{name=@{CharacterName} @{combat_focus}}} {{Damage=[[@{Combat_Focus_Damage}]]}}  {{Crit=[[@{Combat_Focus_Damage}+[[(@{Combat_Focus_Dice}*@{Combat_Focus_Face})]]]]}}  {{Sanity=@{Combat_Focus_Sanity}}}  {{Duration=@{Combat_Focus_Duration}}}  {{Save=@{Combat_Focus_Save} @{Combat_Focus_Type}}}  {{Effect=For the duration when you hit a creature with an attack they must make a **WISDOM SAVING** throw or take an extra 1d8 psychic damage.}}"></button> <input  type="text"     name="attr_Combat_Focus_Name"              class="power_name"      value="Combat Focus"                                                   readonly /> <input  type="text"     name="attr_Combat_Focus_Cast"              class="power_range"     value="Bonus"                                                          readonly /> <input  type="text"     name="attr_Combat_Focus_Range"             class="power_range"     value="Self"                                                           readonly /> <input  type="hidden"   name="attr_Combat_Focus_Sanity_Base_Cost"                          value="2"                                                              disabled="true"/> <input  type="text"   name="attr_Combat_Focus_Sanity"                               value="round(@{Combat_Focus_Sanity_Base_Cost}-@{Powers_Sanity_Reduction})"  disabled="true"/> <input  type="text"     name="attr_Combat_Focus_Sanity_Total"      class="power_damage"                                                                           readonly /> <input  type="text"     name="attr_Combat_Focus_Duration"          class="power_duration"  value="Concentration 1 Minute"                                         readonly /> <input  type="text"     name="attr_Combat_Focus_Damage"            class="power_damage"                                                                           readonly /> <input  type="text"     name="attr_Combat_Focus_Save"              class="power_Save"      value="[[8+@{wis_mod}+@{proficiency}]]"                                disabled="true"/> <input  type="text"     name="attr_Combat_Focus_Type"              class="power_Type"      value="Wisdom"                                                         readonly /> <input  type="text"     name="attr_Combat_Focus_Effect"            class="power_Effect"    value="One Attacked Creature"                                          readonly /> <input  type="hidden"   name="attr_Combat_Focus_Dice"                                                                                                             readonly /> <input  type="hidden"   name="attr_Combat_Focus_Face"                                      value="8"                                                              disabled="true"/> </div> </div> </span> </div> </div> </div> <script type="text/worker"> on('change:combat_focus_sanity', function() { getAttrs(['Combat_Focus_Sanity'], function(values) { const Combat_Focus_Sanity = parseInt(values.Combat_Focus_Sanity) || 2; let Combat_Focus_Sanity_Total = "2"; if (Combat_Focus_Sanity = 1) { Combat_Focus_Sanity_Total = 1; } else if(Combat_Focus_Sanity = 0) { Combat_Focus_Sanity_Total = 0; } else if(Combat_Focus_Sanity = -1) { Combat_Focus_Sanity_Total = 0; } else if(Combat_Focus_Sanity = -2) { Combat_Focus_Sanity_Total = 0; } else if(Combat_Focus_Sanity = -3) { Combat_Focus_Sanity_Total = 0; }  setAttrs({ Combat_Focus_Sanity_Total: Combat_Focus_Sanity_Total }); });  }); </script>
1687308803

Edited 1687314975
vÍnce
Pro
Sheet Author
I'm not a programmer... Use "===" in your IF tests instead of a single "=".&nbsp; I believe the single "=" will try and assign a new value to the variable, whereas "===" is checking to see if they are equal (value and type). <a href="https://www.guru99.com/difference-equality-strict-operator-javascript.html" rel="nofollow">https://www.guru99.com/difference-equality-strict-operator-javascript.html</a> update/edit post Also, I think you could eleminate the need for the multiple IF checks by using Math.max to only except a value of 0 or higher. getAttrs(['Combat_Focus_Sanity'], function (values) { const Combat_Focus_Sanity = parseInt(values.Combat_Focus_Sanity) || 0; let Combat_Focus_Sanity_Total = 0; Combat_Focus_Sanity_Total = Math.max(Combat_Focus_Sanity, 0); setAttrs({ Combat_Focus_Sanity_Total, }); }); Cheers
1687326879

Edited 1687343736
GiGs
Pro
Sheet Author
API Scripter
Vince might claim not to be a programmer, but he is correct here, both on the if statement (don't use single = for comparisons) and the Math.max method. This part is likely to return an error though &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setAttrs({ Combat_Focus_Sanity_Total, }); When the variable and attribute are identical you can ignore one like here, but you should never finish the last line in setAttrs with a comma. This should be one of these two: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setAttrs({ Combat_Focus_Sanity_Total }); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setAttrs({ Combat_Focus_Sanity_Total: Combat_Focus_Sanity_Total });
1687331051

Edited 1687333298
vÍnce
Pro
Sheet Author
Thanks GiGs. I think my linter setup (eslint with prettier) must be wrong. ;-(&nbsp; It gives me an error/warning if I don't include the trailing comma. Found some debate about trailing commas on the stackoverflow forums&nbsp; here and Mozilla's javascript reference . So is this a legit issue or personal preference? I've changed my eslintrc setting to&nbsp;"trailingComma": "none" which has quelched the error/warnings. Cheers
1687343624

Edited 1687343775
GiGs
Pro
Sheet Author
API Scripter
I thought this was is a legit issue. I'm sure it used to cause crashes before, but you can apparently write setAttrs that way now. Note: if you write anything which requires strict JSON, that trailing comma won't be accepted and as noted above, I am sure that setAttrs used to work that way (though standard javascript was fine with it). It works now, though. Maybe I was wrong about the way it used to work? I've given advice before to avoid a trailing comma, I'll have to change that practice.
so that did not work and i have tried a few more things but still not working so i am adding the updated script. and a pic to show what i was trying to do. &lt;div class="sheet-magic"&gt;&lt;!-- Magic --&gt; &lt;span&gt; &lt;div class="Powers"&gt; &lt;h1&gt;Simple Powers&lt;/h1&gt; &lt;input&nbsp; type="text"&nbsp; class="power_damage"&nbsp; name="attr_Powers_Sanity_Reduction"&nbsp; &nbsp; &nbsp; &nbsp; value="(0-@{PsychicGift_Checkbox}-@{PsychicTraining_Checkbox}-@{AdvancePsychicTraining_Checkbox})"&nbsp; disabled="true"/&gt; &lt;input&nbsp; type="text"&nbsp; class="power_damage"&nbsp; name="attr_Combat_Focus_Sanity_Base_Cost"&nbsp; value="2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;disabled="true"/&gt; &lt;input&nbsp; type="text"&nbsp; class="power_damage"&nbsp; name="attr_Combat_Focus_Sanity_Total"&nbsp; &nbsp; &nbsp; value="[[@{Combat_Focus_Sanity_Base_Cost}+@{Powers_Sanity_Reduction}]]"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;disabled="true"/&gt; &lt;div class="Heading"&gt; &lt;h4&gt;&amp;nbsp &lt;/h4&gt; &lt;h4&gt;&amp;nbsp &lt;/h4&gt; &lt;h4&gt;Power&lt;/h4&gt; &lt;h4&gt;Cast&lt;/h4&gt; &lt;h4&gt;Range&lt;/h4&gt; &lt;h4&gt;Sanity&lt;/h4&gt; &lt;h4&gt;Duration&lt;/h4&gt; &lt;h4&gt;Damage&lt;/h4&gt; &lt;h4&gt;Save&lt;/h4&gt; &lt;h4&gt;Type&lt;/h4&gt; &lt;h4&gt;Effect&lt;/h4&gt; &lt;/div&gt; &lt;div class="CombatFocus"&gt; &lt;input&nbsp; type="checkbox" name="attr_Combat_Focus"&nbsp; value="1"&gt; &lt;button type="roll" value="&amp;{template:default} {{name=@{CharacterName} @{combat_focus}}} {{Damage=[[@{Combat_Focus_Damage}]]}}&nbsp; {{Crit=[[@{Combat_Focus_Damage}+[[(@{Combat_Focus_Dice}*@{Combat_Focus_Face})]]]]}}&nbsp; {{Sanity=@{Combat_Focus_Sanity}}}&nbsp; {{Duration=@{Combat_Focus_Duration}}}&nbsp; {{Save=@{Combat_Focus_Save} @{Combat_Focus_Type}}}&nbsp; {{Effect=For the duration when you hit a creature with an attack they must make a **WISDOM SAVING** throw or take an extra 1d8 psychic damage.}}"&gt;&lt;/button&gt; &lt;input&nbsp; type="text"&nbsp; &nbsp; &nbsp;name="attr_Combat_Focus_Name"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="power_name"&nbsp; &nbsp; &nbsp; value="Combat Focus"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;readonly /&gt; &lt;input&nbsp; type="text"&nbsp; &nbsp; &nbsp;name="attr_Combat_Focus_Cast"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="power_range"&nbsp; &nbsp; &nbsp;value="Bonus"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readonly /&gt; &lt;input&nbsp; type="text"&nbsp; &nbsp; &nbsp;name="attr_Combat_Focus_Range"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;class="power_range"&nbsp; &nbsp; &nbsp;value="Self"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;readonly /&gt; &lt;input&nbsp; type="text"&nbsp; &nbsp; &nbsp;name="attr_Combat_Focus_Sanity"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="power_damage"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;readonly /&gt; &lt;input&nbsp; type="text"&nbsp; &nbsp; &nbsp;name="attr_Combat_Focus_Duration"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="power_duration"&nbsp; value="Concentration 1 Minute"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;readonly /&gt; &lt;input&nbsp; type="text"&nbsp; &nbsp; &nbsp;name="attr_Combat_Focus_Damage"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="power_damage"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;readonly /&gt; &lt;input&nbsp; type="text"&nbsp; &nbsp; &nbsp;name="attr_Combat_Focus_Save"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="power_Save"&nbsp; &nbsp; &nbsp; value="[[8+@{wis_mod}+@{proficiency}]]"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; disabled="true"/&gt; &lt;input&nbsp; type="text"&nbsp; &nbsp; &nbsp;name="attr_Combat_Focus_Type"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="power_Type"&nbsp; &nbsp; &nbsp; value="Wisdom"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;readonly /&gt; &lt;input&nbsp; type="text"&nbsp; &nbsp; &nbsp;name="attr_Combat_Focus_Effect"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="power_Effect"&nbsp; &nbsp; value="One Attacked Creature"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readonly /&gt; &lt;input&nbsp; type="hidden"&nbsp; &nbsp;name="attr_Combat_Focus_Dice"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;readonly /&gt; &lt;input&nbsp; type="hidden"&nbsp; &nbsp;name="attr_Combat_Focus_Face"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value="8"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; disabled="true"/&gt; &lt;/div&gt; &lt;/div&gt; &lt;/span&gt; &lt;/div&gt;
1687354507
GiGs
Pro
Sheet Author
API Scripter
Can you remove everything from the code except what affects that sanity box?
So you just want the line that have to do with sanity cost? I will add them below.&nbsp; &lt;!-- these are two that are added to get the value --&gt; &lt;input&nbsp; type="text"&nbsp; class="power_damage"&nbsp; name="attr_Powers_Sanity_Reduction" value="(0-@{PsychicGift_Checkbox}-@{PsychicTraining_Checkbox}-@{AdvancePsychicTraining_Checkbox})"&nbsp; disabled="true"/&gt; &lt;input type="text" class="power_damage" name="attr_Combat_Focus_Sanity_Base_Cost" value="2" disabled="true"/&gt; &lt;!-- This is the line where the two are added together --&gt; &lt;input type="text" class="power_damage" name="attr_Combat_Focus_Sanity_Total" value="[[@{Combat_Focus_Sanity_Base_Cost}+@{Powers_Sanity_Reduction}]]" disabled="true"/&gt; &lt;!-- This is the line that i would like to show a 0 event if the total is a negative number --&gt; &lt;input type="text" name="attr_Combat_Focus_Damage" class="power_damage" readonly /&gt; I hope this is what was needed.&nbsp;
1687359175

Edited 1687441195
GiGs
Pro
Sheet Author
API Scripter
Two of those have disabled="true" Sheet workers cannot change any disabled attributes, thats why you set them to readonly instead. Also they are autocalc fields - if you are using sheet workers, you should calculate those values in a sheet worker. The sheet worker wont see the calculated number, it will see the text inside the value="text here". It can't calculate that value. Looking back to your original post, this is a disabled autocalc field: &lt;input&nbsp; type="text"&nbsp; &nbsp;name="attr_Combat_Focus_Sanity"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value="round(@{Combat_Focus_Sanity_Base_Cost}-@{Powers_Sanity_Reduction})"&nbsp; disabled="true"/&gt; A sheet worker can do nothing with that. You'd need to calculate its value with a sheet worker (and change it to readonly). Once you start using sheet workers, everything that contributes to a sheet worker must be calculated in sheet workers-&nbsp; you cannot use autocalc attributes in any of the constituent attributes.
1687367167
vÍnce
Pro
Sheet Author
I believe the sheetworker should should be fine(tested by providing raw values in the html), so definitely something in the HTML as GiGs pointed out. @GIGs +1