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

November 25 (6 years ago)

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?

November 25 (6 years ago)

Edited November 25 (6 years ago)
GiGs
Pro
Sheet Author
API Scripter

This really simple no-frills script should do what you need:

on("change:token", function(obj) {
    const bar = 3; // change this to the number you use for the HP bar
    let maxValue = parseInt(obj.get(`bar${bar}_max`));
    let curValue = parseInt(obj.get(`bar${bar}_value`));
    if (!isNaN(maxValue) && !isNaN(curValue) && curValue > maxValue) {
        obj.set(`bar${bar}_value`,maxValue);
    }
});
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.

November 25 (6 years ago)
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) {
    for(let i = 1; i <= 3; i++) {
        let maxValue = parseInt(obj.get(`bar${i}_max`));
        let curValue = parseInt(obj.get(`bar${i}_value`));
        if (!isNaN(maxValue) && !isNaN(curValue) && curValue > maxValue) {
            obj.set(`bar${i}_value`,maxValue);
        }    
    }
});

November 25 (6 years ago)
Thank you. :)