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 to enforce max in token bar values

I'm looking for a script that will prevent the entry of more HP than what the max is set to be. For instance if the value|max of bar1 is 10|12 and a player (or myself?) enters "+5" the new value of bar1 will be 12. This is not only for when my players use healing of any sort, but I am planning to drop a troll on them and I'd love to have an easy way to maintain that without having to check what the max value is. Any ideas, or is this another script I'll have to write myself?
1543126929

Edited 1543126989
GiGs
Pro
Sheet Author
API Scripter
This really simple no-frills script should do what you need: on("change:token", function(obj) { &nbsp; &nbsp; const bar = 3; // change this to the number you use for the HP bar &nbsp; &nbsp; let maxValue = parseInt(obj.get(`bar${bar}_max`)); &nbsp; &nbsp; let curValue = parseInt(obj.get(`bar${bar}_value`)); &nbsp; &nbsp; if (!isNaN(maxValue) &amp;&amp; !isNaN(curValue) &amp;&amp; curValue &gt; maxValue) { &nbsp; &nbsp; &nbsp; &nbsp; obj.set(`bar${bar}_value`,maxValue); &nbsp; &nbsp; } }); There's a more involved script posted ages ago that shows how you can add more options. This one is designed for chat input, but could be adapted to add extra features, like giving chat updates when a character drops below zero, or is injured/healed, etc. <a href="https://app.roll20.net/forum/post/1216260/script-alter-bar-on-token" rel="nofollow">https://app.roll20.net/forum/post/1216260/script-alter-bar-on-token</a>
1543127254
GiGs
Pro
Sheet Author
API Scripter
And if you just want to constrain all 3 bars to max, if they have one: on("change:token", function(obj) { &nbsp; &nbsp; for(let i = 1; i &lt;= 3; i++) { &nbsp; &nbsp; &nbsp; &nbsp; let maxValue = parseInt(obj.get(`bar${i}_max`)); &nbsp; &nbsp; &nbsp; &nbsp; let curValue = parseInt(obj.get(`bar${i}_value`)); &nbsp; &nbsp; &nbsp; &nbsp; if (!isNaN(maxValue) &amp;&amp; !isNaN(curValue) &amp;&amp; curValue &gt; maxValue) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.set(`bar${i}_value`,maxValue); &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; } });
Thank you. :)