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

Input max value using scripts

Hello,  Im am creating a sheet the has some buttons that change the value other inputs. Like the health bar. when you click the button the it will automatically add to the HP value. it works great. Now i'm trying to add some limits to it, such as a may value that the button will not exceed. Example My Max HP is 46 and i only too 4 damage before a long rest. I recover 16 HP per long rest so when i click the long rest button i don't want the Hp to exceed 46. At the moment if i was down 4 hp and i clicked the Long Rest button i would have 58 Hp which is above my max. I will add the button im using to add to my HP below. Let me know if you need anything else.  Thanks, This is the button am using to add to my HP and a few other fields. It does the same thing for the other fields but i figured if i can fix on i can fix the others. <script type="text/worker"><!-- Long Rest Button - This changes the value of the Hit Dice, HP, Exhaustion Level, and Sanity when the player clicks the long rest button--> on('clicked:long_rest', function() { getAttrs(['sanity_recovery', 'current_sanity', 'long_rest_exhaustion', 'current_exhaustion', 'long_rest_hp', 'current_hp', 'level', 'hit_dice'], function(values) { const level = values.level ||0; const new_hit_dice = level ||0; const current_hp = values.current_hp ||0; const long_rest_hp = values.long_rest_hp ||0; const new_rest_hp = current_hp + long_rest_hp ||0; const current_exhaustion = values.current_exhaustion ||0; const long_rest_exhaustion = values.long_rest_exhaustion ||0; const new_rest_exhaustion = current_exhaustion - long_rest_exhaustion ||0; const current_sanity = values.current_sanity ||0; const sanity_recovery = values.sanity_recovery ||0; const new_sanity_recovery = current_sanity + sanity_recovery ||0; setAttrs({hit_dice: new_hit_dice}); setAttrs({current_hp: new_rest_hp}); setAttrs({current_exhaustion: new_rest_exhaustion}); setAttrs({current_sanity: new_sanity_recovery}); }); }); </script>
1717103989
Stephen C.
Pro
Sheet Author
If you need to do this check whenever you are increasing the HP, that sounds like a great time to make a function for that! var checkHP = function() { getAttrs(["current_hp","max_hp"], function(values) { let currenthp = parseInt(values.current_hp); let maxhp = parseInt(values.max_hp)||0; if(maxhp != 0 && currenthp > maxhp) { setAttrs({"current_hp":maxhp}); } }); }; Hasn't been tested, but it's pretty simple logic. If the current HP is greater than the max HP, then you just set the current to the maximum allowed value. Otherwise, you do nothing. I added another condition in there so that it wouldn't do this if the max HP is 0, such as at character creation. If someone wanted to enter their current HP but it kept getting reset because they had to set their max HP first, that might get a bit frustrating. I don't know if you actually need that, though.
That did not work but that could be because of me. I added it and it does not seem to work not sure why.  Before I pressed the button After i pressed the button <script type="text/worker"><!-- Long Rest Button - This changes the value of the Hit Dice, HP, Exhaustion Level, and Sanity when the player clicks the long rest button--> on('clicked:long_rest', function() { getAttrs(['sanity_recovery', 'current_sanity', 'long_rest_exhaustion', 'current_exhaustion', 'long_rest_hp', 'current_hp', 'level', 'hit_dice'], function(values) { const level = values.level ||0; const new_hit_dice = level ||0; const current_hp = values.current_hp ||0; const long_rest_hp = values.long_rest_hp ||0; const new_rest_hp = current_hp + long_rest_hp ||0; const current_exhaustion = values.current_exhaustion ||0; const long_rest_exhaustion = values.long_rest_exhaustion ||0; const new_rest_exhaustion = current_exhaustion - long_rest_exhaustion ||0; const current_sanity = values.current_sanity ||0; const sanity_recovery = values.sanity_recovery ||0; const new_sanity_recovery = current_sanity + sanity_recovery ||0; setAttrs({hit_dice: new_hit_dice}); setAttrs({current_hp: new_rest_hp}); setAttrs({current_exhaustion: new_rest_exhaustion}); setAttrs({current_sanity: new_sanity_recovery}); }); }); var checkHP = function() { getAttrs(["current_hp","max_hp"], function(values) { let currenthp = parseInt(values.current_hp); let maxhp = parseInt(values.max_hp)||0; if(maxhp != 0 && currenthp > maxhp) { setAttrs({"current_hp":maxhp}); } }); }; </script>
1717168627

Edited 1717168744
GiGs
Pro
Sheet Author
API Scripter
I may be seconding advice already given. Each attribute has a default value (current value), but can also be given a max value - this is good for tokens, since you can choose that attribute, say hp, and it will automatically show the current and max values. Say the current value is @{hp), the max would be @{hp_max} So you could create them in a saheet worker, like setAttrs({ hp: 10, hp_max: 10 }; Whenever you would refer to an attribute, just add _max to the name ( after the name, no before it), like getAttrs(['hp', 'hp_max']), values => { The _max attribute is a completely separate attribute, and you adjust its values separately. This means when hp is reduced by damage or increased by healing, the max isn't affected. If you want to limit the score to your maximum, as with healing, you can use the Math.min() function. Lets say you are adding 10 to current HP but dont want it to go over maximum, you could do this: pn('clicked:heal_by_10, () => { getAttrs(['hp', 'hp_max'), values=> { let hp = values.hp; let max = values.hp_max: hp = Math.min(hp +10, max); setAttrs({ hp:hp }); }); });
1717169121
GiGs
Pro
Sheet Author
API Scripter
Looking at your last sheet workers, they are written very ineffiicently, but they should work. So we need to see the html for these attributes. I am wonderinf if (number 1 problem in this kind of situation) you are using autocalc attributes in that html. Disabled inputs, often called autocalcs, are completely incompatible with sheet workers. If you have any disabled inputs you will have to rework them so the values are cqalculated with sheet workers, anfd keep working so they rae no disabled inputs involved in calculating any attribtes leading up to your calculation.