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

Can anyone fix this script for me please - HP announcement

1518035514

Edited 1518035600
So this script is very nice, however the scriptwriter is no longer on roll20. Anyways there is 1 issue I am having and I do not know how to disable it. Example to follow - Put a token - on bar1 make sure it says "Hp" for it to work. not HP or hp or hP now once you have that set - let's use 100/100 for bar 1 value. When you click on the token and change the green circle Hp attribute from 100 to 3, works as intended When you click on the token and change the green circle Hp attribute from 100 with "-97" it will change to 3 - works as intended When you click on the token and change the green circle Hp attribute from 3 with "+5" it will change to 8 - works as intended When you click on the token and change the green circle Hp attribute from 3 to 8, it will change the value to 100 --- NOT INTENDED personally - 4 years on Roll 20 - I do not use +/- when changing my values. I just simply put the total and so do many other players I assume. I do not know what value/line is causing the number to reset back to the max value. Can any of you voodoo script masters fix this small issue please?? SCRIPT below on("change:attribute", function(obj, prev) { if(obj.get("name") !== "hp") return; var damageTaken = 0; var currenthp =0; var oldhp = 0; var max = 0; var charName = getObj("character", obj.get("_characterid")); currenthp = obj.get("current"); oldhp = prev["current"]; max = obj.get("max"); if (currenthp > max) { currenthp = max; obj.set("current", currenthp); } damageTaken = currenthp - oldhp; if (damageTaken < 0 ) { sendChat(charName.get("name"), "/me has taken " + -1 * damageTaken + " damage"); } if (damageTaken > 0 ) { sendChat(charName.get("name"), "/me has healed " + damageTaken + " damage"); } }); on("change:graphic:bar1_value", function(obj, prev) { //Ignores players, handled above if(obj.get("represents") != "") return; if (Campaign().get("initiativepage") == false) return; var damageTaken = 0; var currenthp =0; var oldhp = 0; var max = 0; var charName = obj.get("name"); currenthp = obj.get("bar1_value"); oldhp = prev["bar1_value"]; max = obj.get("bar1_max"); if (currenthp > max) { currenthp = max; obj.set("bar1_value", currenthp); } damageTaken = currenthp - oldhp; if (damageTaken < 0 ) { sendChat(charName, "/me has taken " + -1 * damageTaken + " damage"); } if (damageTaken > 0 ) { sendChat(charName, "/me has healed " + damageTaken + " damage"); } });
1518037716

Edited 1519414168
Ada L.
Marketplace Creator
Sheet Author
API Scripter
When getting the current or max value of a numerical attribute, you should always use parseInt on it. Otherwise, you could get a string instead of a number. In your example, "8" is greater than "100" lexically, so your comparison passed and it set the HP to "100". E.g.: currenthp = parseInt( obj.get("bar1_value") ); oldhp = parseInt( prev["bar1_value"] ); max = parseInt( obj.get("bar1_max") ); Here's a fixed version of the entire script: on("change:attribute", function(obj, prev) {     if (obj.get("name") !== "hp") return;          var damageTaken = 0;     var currenthp = 0;     var oldhp = 0;     var max = 0;     var charName = getObj("character", obj.get("_characterid"));     currenthp = parseInt(obj.get("current"));     oldhp = parseInt(prev["current"]);     max = parseInt(obj.get("max"));     if (currenthp > max) {         currenthp = max;         obj.set("current", currenthp);     }     damageTaken = currenthp - oldhp;     if (damageTaken < 0) {         sendChat(charName.get("name"), "/me has taken " + -1 * damageTaken + " damage");     }     if (damageTaken > 0) {         sendChat(charName.get("name"), "/me has healed " + damageTaken + " damage");     } }); on("change:graphic:bar1_value", function(obj, prev) {     //Ignores players, handled above     if (obj.get("represents") != "") return;     if (Campaign().get("initiativepage") == false) return;          var damageTaken = 0;     var currenthp = 0;     var oldhp = 0;     var max = 0;     var charName = obj.get("name");     currenthp = parseInt(obj.get("current"));     oldhp = parseInt(prev["current"]);     max = parseInt(obj.get("max"));     if (currenthp > max) {         currenthp = max;         obj.set("bar1_value", currenthp);     }     damageTaken = currenthp - oldhp;     if (damageTaken < 0) {         sendChat(charName, "/me has taken " + -1 * damageTaken + " damage");     }     if (damageTaken > 0) {         sendChat(charName, "/me has healed " + damageTaken + " damage");     } }); In the future, when sharing samples of code, please include them inside a code block like those above. This will preserve the whitespace in the code and make it easier to read. ....