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

[API Help] Script for Turning Undead

I'm new to javascript, as well as to roll20. I'm attempting to make a script that after typing !Turn into the chat bar, will shoot out the maximum number of HD that a cleric can turn based off of the d20, charisma modifier, and a +2 bonus to his knowledge religion ranks of 5 or more. I have had some issues finding the exact answer in the forums. var character = getObj("character", "Traovepo"); getAttrByName("Traovepo", "cha-mod"); getAttrByName("Traovepo", "level"); var chaMod = getAttrByName("Traovepo", "cha-mod"); var clericLevel = getAttrByName("Traovepo", "level"); var dice = Math.floor((Math.random() * 20) + 1); on("chat:message", function(msg) { if (msg.type == "api" && msg.content.indexOf("!Turn") !== -1) { if (dice + chaMod + 2 <= 0) {sendChat("Max HD Affected: " + clericLevel -4)} if (dice + chaMod + 2 == 1||2||3) {sendChat("Max HD Affected: " + clericLevel - 3)}; if (dice + chaMod + 2 == 4||5||6) {sendChat("Max HD Affected: " + clericLevel - 2)}; if (dice + chaMod + 2 == 7||8||9) {sendChat("Max HD Affected: " + clericLevel - 1)}; if (dice + chaMod + 2 == 10||11||12) {sendChat("Max HD Affected: " + clericLevel)}; if (dice + chaMod + 2 == 13||14||15) {sendChat("Max HD Affected: " + clericLevel + 1)}; if (dice + chaMod + 2 == 16||17||18) {sendChat("Max HD Affected: " + clericLevel + 2)}; if (dice + chaMod + 2 == 19||20||21) {sendChat("Max HD Affected: " + clericLevel + 3)}; if (dice + chaMod + 2 >= 22) {sendChat("Max HD Affected: " + clericLevel + 4)}; } }); my test character's name is Traovepo. I'm sure there are multiple syntax errors, but the one that I'm getting snagged on at the moment is the following error messages "Invalid character_id Traovepo for getAttrByName()" "Invalid character_id Traovepo for getAttrByName()" "Invalid character_id Traovepo for getAttrByName()" I'm trying to use the character sheet to call the Cha-mod, Level so that when the player types "!Turn" it automates everything and sends a chat of the maximum number of Hit Die he can affect. All rules based off of D&D 3.5 Thanks in advance for your assistance
1452121253

Edited 1452122048
Learning javascript by coding for Roll20 campaigns is awesome, kudos! I'm sure a javascript expert will stop by in no time and provide some guidance and insight. In case you might be interested, the PowerCards API script is really adept at simple conditional logic—I cannot recommend it higher! Here's what the turn check might look like with PowerCards: !power {{ --Turning Check|[[ [$R] 1d20 + @{Traovepo|cha-mod} + 2 ]] -- ?? $R <= 0 ?? Max HD Affected|[[ @{Traovepo|level} - 4 ]] -- ?? $R >= 1 AND $R <= 3 ?? Max HD Affected|[[ @{Traovepo|level} - 3 ]] -- ?? $R >= 4 AND $R <= 6 ?? Max HD Affected|[[ @{Traovepo|level} - 2 ]] -- ?? $R >= 7 AND $R <= 9 ?? Max HD Affected|[[ @{Traovepo|level} - 1 ]] -- ?? $R >= 10 AND $R <= 12 ?? Max HD Affected|[[ @{Traovepo|level} ]] -- ?? $R >= 13 AND $R <= 15 ?? Max HD Affected|[[ @{Traovepo|level} + 1 ]] -- ?? $R >= 16 AND $R <= 18 ?? Max HD Affected|[[ @{Traovepo|level} + 2 ]] -- ?? $R >= 19 AND $R <= 21 ?? Max HD Affected|[[ @{Traovepo|level} + 3 ]] -- ?? $R >= 22 ?? Max HD Affected|[[ @{Traovepo|level} + 4 ]] }}
I appreciate the response, I'll have to look into the Power cards, just browsing through it looks like there's a lot of possibilities there
1452122439

Edited 1452122564
Indeed, Roll20 has a ton of tools! PowerCards is one of the most popular API scripts (and for good reason). Here's another way to acquire the above, showcasing the capacities of Roll20's math functions (sans API): Max HD Affected: [[ @{Traovepo|level} - ({4 - floor(({1d20 + @{Traovepo|cha-mod} + 2, 0d0}kh1 + 2) / 3), 4 + 0d0}kl1) ]]
1452143788
The Aaron
Pro
API Scripter
getObj() expects a character ID. To get a character by name you need to use findObjs(): var character = findObjs({type: "character", name:"Traovepo"})[0]; findObjs() takes an object and returns all the Roll20 objects with the same properties and values. (Note the 's' on the end of findObjs().  Also the [0] after the call to get the first one from the array result. ) Since this is just for testing anyway, you might be better off to copy the character ID from chat and use it with getObj(). Type in chat: @{Traovepo|character_id} and copy the result. (Will be something like -JskHd4784GdT4gj377). You can then use that with getObj(): var char = getObj('character', '-JskHd4784GdT4gj377'); Hopefully that will get you jump started. Be sure to ask any other questions!
Thanks so much! I'm been trying to get that character ID for so long! not going lie, feeling pretty stupid right now
1452178586
The Aaron
Pro
API Scripter
Don't!  It's super confusing.  It took me quite a while to wrap my brain around it.  (and you wouldn't believe how often I type findObj() and get an error! (which is why I pointed that out. =D) ). Definitely post back if you have any more questions, no matter how small!  =D