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't automatically increase max hp when CON modifier increases.

1711772645

Edited 1711772679
on("chat:message", function(msg) { if(msg.type == "api" && msg.content.indexOf("!dogman") !== -1) { var selected = msg.selected; if(selected) { for(var i = 0; i < selected.length; i++) { var token = getObj("graphic", selected[i]._id); if(token) { var characterId = token.get("represents"); var character = getObj("character", characterId); if(character) { // Append the subrace to the "race" attribute var raceAttribute = findObjs({ type: 'attribute', characterid: characterId, name: 'race' })[0]; var currentRace = raceAttribute ? raceAttribute.get("current") : ""; if(raceAttribute) { raceAttribute.set({ current: "Dogman" }); } else { // If the race attribute does not exist, create it with the subrace appended createObj("attribute", { name: "race", current: "Dogman", characterid: characterId }); } // Increase Constitution by 2 var constitutionAttribute = findObjs({ type: 'attribute', characterid: characterId, name: 'constitution' })[0]; if (constitutionAttribute) { var currentConstitution = parseInt(constitutionAttribute.get("current")) || 0; constitutionAttribute.set({ current: currentConstitution + 2 }); } else { createObj("attribute", { name: "constitution", current: "2", // Assumes no base constitution score; adjust as necessary characterid: characterId }); } // Calculate the increase in hit points var levelAttribute = findObjs({ type: 'attribute', characterid: characterId, name: 'level' })[0]; var currentHPAttribute = findObjs({ type: 'attribute', characterid: characterId, name: 'hp' })[0]; // Adjust 'hp' if needed var maxHPAttribute = findObjs({ type: 'attribute', characterid: characterId, name: 'hp_max' })[0]; // Adjust 'hp_max' as per your sheet if (levelAttribute && currentHPAttribute && maxHPAttribute) { var level = parseInt(levelAttribute.get("current")) || 0; var currentHP = parseInt(currentHPAttribute.get("current")) || 0; var maxHP = parseInt(maxHPAttribute.get("current")) || 0; var hpIncrease = level; // Increase HP by 1 for each level currentHPAttribute.set({ current: currentHP + hpIncrease }); maxHPAttribute.set({ current: maxHP + hpIncrease }); // Send a confirmation message sendChat("API", "/w gm Updated hit points for " + character.get("name") + " due to increased Constitution modifier."); } // Increase Dexterity by 1 var dexterityAttribute = findObjs({ type: 'attribute', characterid: characterId, name: 'dexterity' })[0]; if (dexterityAttribute) { var currentDexterity = parseInt(dexterityAttribute.get("current")) || 0; dexterityAttribute.set({ current: currentDexterity + 1 }); } else { createObj("attribute", { name: "dexterity", current: "1", // Assumes no base dex score; adjust as necessary characterid: characterId }); } // Send a confirmation message sendChat("API", "/w gm Added homebrew race to " + character.get("name")); } } } } else { sendChat("API", "/w gm No tokens selected."); } } }); Basically, I am trying to make a custom race that has a +2 con and +1 dex (using the standard DnD 5e sheet). Everything works except the max hp doesn't increase when the con modifier changes. The part that is coming back undefined but appears correct according to the character sheet, is this:   var maxHPAttribute = findObjs({ type: 'attribute', characterid: characterId, name: 'hp_max' })[0];
1711802820
GiGs
Pro
Sheet Author
API Scripter
I'm wondering if it's come back undefined when the player hasn't maually entered a value. If it is undefined you may need to recalculate it. IIRC, hp max doesnt depend on your current stats - you can just recalculate it from the relevant attributes. Don't bother grabbing it from the sheet, just rebuild it from scratch.
If you are looking to edit some existing D&D 5e characters, you don't need to write your own Mod, if you don't want. Here is an example using ScriptCards !script {{ --&CustomRace|Dogman --&MainAttribute|constitution --&SecondaryAttribute|dexterity --&MainAttributeIncrease|2 --&SecondaryAttributeIncrease|1 --#whisper|gm --#title|[&CustomRace] Change --%tid|foreach;SC_SelectedTokens --+[*[&tid]:t-name]|now a [&CustomRace] --!a:[*[&tid]:character_id]|[&MainAttribute]:+=[&MainAttributeIncrease]|[&SecondaryAttribute]:+=[&SecondaryAttributeIncrease]|hp:+=[*[&tid]:level]|hp^:+=[*[&tid]:level] --%| }} That will loop over the selected tokens and increase the main attribute by 2, the secondary attribute by 1, and the hp (both current and max) by the current level. I'm more familiar with ScriptCards but I'm sure this could also be done with ChatSetAttr and some Meta-Script Toolbox like SelectManager and Fetch as well but I'm less proficient in Meta-Script. One thing to note about Roll20 Attributes  is they have a current property and a max property. So you wouldn't necessarily lookup hp_max like that. Mods handle this different ways. Like in the above example, ScriptCards uses the caret `^` to access the max property. Whereas ChatSetAttr would use a secondary pipe like so: !setattr --sel --mod --hp|+5|+5 where the first value would be for current and the later for the max.
1711860969

Edited 1711861153
In case anyone wants to use something like this, the way to access the max hp value is in the hp object. In the code above, I have currentHPAttribute. When you log that variable it gives you.  {"name":"hp","current":"12","max":"12","_id":"-NuCQwhW3l-ZI0cWMFP5","_type":"attribute","_characterid":"-NuCQ6FJhsIjCdnuBnhL"} So you can see max is a property of the hp object. You can increase your max hp by rewriting the hp updating section of the code like so: if (hpAttribute) { var levelAttribute = findObjs({ type: 'attribute', characterid: characterId, name: 'level' })[0]; if (levelAttribute) { var level = parseInt(levelAttribute.get("current")) || 1; // Default to 1 if level is undefined var hpIncrease = level; // Increase HP by 1 for each level var currentHP = parseInt(hpAttribute.get("current")) || 0; var maxHP = parseInt(hpAttribute.get("max")) || currentHP; // Use current HP as default if max is undefined hpAttribute.set({ current: currentHP + hpIncrease, max: maxHP + hpIncrease }); // Send a confirmation message sendChat("API", "/w gm Updated hit points for " + character.get("name") + " due to increased Constitution modifier."); } } Edit: Also, change the currentHPAttribute variable to: var hpAttribute = findObjs({ type: 'attribute', characterid: characterId, name: 'hp' })[0]; // Adjust 'hp' if needed