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 .
×
Why do rogues wear leather armor? Because it’s made of hide. 🥷

[Script] - Change attribute value onclick

Hello there, I'm working on the following script : If a checkbox named "attr_fatigue_rouge" is checked, the MAX value of sante is divide by 2 If the checkbox is uncheck, the MAX sante value come back to normal by multiplying it by 2 However, the script don't work, I'm a newby starting to develop my own character sheet and I don't understand why. There is my checkbox HTML : < div class = "grid-carton" >     < div > Etat de fatigue         <!-- A coder : fatigué = -30% aux stats, Ereinté = stats ingame divisées par 2 -->         < input type = 'checkbox' class = "sheet-carton-jaune" name = 'attr_fatigue_jaune' aria-label = "Fatigué" />         < input type = 'checkbox' class = "sheet-carton-rouge" name = 'attr_fatigue_rouge' aria-label = "Ereinté" />     </ div > </ div > < div class = "grid-stats-pv-value" >     < input type = "number" class = "sheet-number-main" name = "attr_sante" >     /     < input type = "number" class = "sheet-number-bar-max" name = "attr_sante_max" > </ div > My script :     on ( 'change:fatigue_rouge' , function () {         getAttrs ([ "sante_max" ], function ( values ) {             let sante = parseInt ( values . sante_max )|| 0 ;             if ( fatigue_rouge == on ) {                 setAttrs ({                         sante_max : Math . floor ( newsante / 2 )                                       });             } else {                 setAttrs ({                         sante_max : newsante * 2                                       });             }         });     }); If anyone can explain me where I'm wrong, thank you !
1637523652
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
You are comparing fatigue_rouge to another variable named on. You want to compate it to the string "on". Additionally, you haven't gotten the value of fatigue_rouge anywhere. You need to get it either from the event itself (not my preferred method), or by adding it to the array passed to getAttrs. This would look like so:     on('change:fatigue_rouge', function() {         getAttrs(['fatigue_rouge',"sante_max"], function(values) {             let sante = parseInt(values.sante_max)||0;             if (values.fatigue_rouge == 'on') {                 setAttrs({                          sante_max: Math.floor(newsante / 2)                                        });             } else {                 setAttrs({                          sante_max: newsante * 2                                        });             }         });     });
1637527651
GiGs
Pro
Sheet Author
API Scripter
The above script refers to a newsante variable, but I think it should be using sante . Personally I suggest having the full value of sante_max stored in a hidden attribute, and add this to the getAttrs and change line, then use x1 and x0.5 to store the sante_max value. This is because if there's ever an error with your logic, you can end up with an incorrect sante_max value, and not be able to get it back easily. Also, it looks like people can manually edit the sante_max attribute which also presents a problem - what if they edit it when its at half value, and they input the correct value, which is then doubled. It's hard to know how to handle this properly without knowing mre about how your system works - how is sancte calculated, for example. I'd be inclined to have 4 stats, something like: sancta, sancta_current, sancta_max, and sancta_current_max (names might change). People can enter the actual values in sancta and sancta_max, but the current values are actually changed during play - and for the max, that's what gets halved and maximised in the above worker.
Thank you both it works !     on('change:fatigue_rouge', function() {         getAttrs(['fatigue_rouge',"sante_max"], function(values) {             let sante = parseInt(values.sante_max)||0;             if (values.fatigue_rouge == 'on') {                 setAttrs({                         sante_max: Math.floor(sante / 2)                                       });             } else {                 setAttrs({                         sante_max: Math.floor(sante * 2)                                       });             }             console.log(values.sante_max)         });     }); I'll follow your advise GiGs and use an hidden attribute to store the MAX values, and maybe put the MAX input as readonly ^^