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

[Help] Retrieval of Attributes in API scripts fails when using multiple tokens with the same character sheet

In the example below I have 2 character sheets: 1. Black Bear. There is one token on the page. 2. Goblin. I have three tokens on the page. I have the following macro setup on my character sheet !attackweapon @{wpn_name},@{npc_wpn_total},?{Modifier|0},@{npc_char_name},0,?{Ambush?|No|Yes},@{target|token_id} The API script that is called is as follows on("chat:message", function(msg) { //This allows players to enter !sr <number> to roll a number of d6 dice with a target of 4. if(msg.type == "api" && msg.content.indexOf("!attackweapon ") !== -1) { var params = msg.content.replace("!attackweapon ", ""); var parms_split = params.split(','); var wpn_val = parms_split[0]; var wpn_skill_total = parms_split[1]; var attack_mods = parms_split[2]; var char_name = parms_split[3]; var ambush_skill = parms_split[4]; var ambush_attack = parms_split[5]; var token_id = parms_split[6]; log(char_name+" - !attackweapon - params: " + params); log(char_name+" - !attackweapon - wpn_name: " + wpn_val); log(char_name+" - !attackweapon - wpn_total: " + wpn_skill_total); log(char_name+" - !attackweapon - modifiers: " + attack_mods); log(char_name+" - !attackweapon - ambush_skill: " + ambush_skill); log(char_name+" - !attackweapon - ambush_attack: " + ambush_attack); log(char_name+" - !attackweapon - token_id: " + token_id); var target = getObj('graphic', token_id); log(char_name+" - !attackweapon - target: " + target); var character = getObj('character', target.get('represents')); log(char_name+" - !attackweapon - character: " + character); var target_armour = getAttrByName(character.id, 'main_armour'); log(char_name+" - !attackweapon - target_armour: " + target_armour); var target_name = getAttrByName(character.id, 'char_name'); log(char_name+" - !attackweapon - target_name: " + target_name); var resistance = getAttrByName(character.id, 'res_def_total'); log(char_name+" - !attackweapon - resistance: " + resistance); if (ambush_attack == "Yes") {wpn_skill_total = +wpn_skill_total + +ambush_skill;} var damage = AT(wpn_val, wpn_skill_total, target_armour, attack_mods, resistance, char_name); var out_dam = damage[0]; var out_fum = damage[1]; var out_pri = damage[2]; var out_sec = damage[3]; var at_roll = damage[4]; if (out_dam == -1) { out_message = "With a roll of " + at_roll + " has fumbled."; sendChat('', "&{template:default} {{name="+char_name+" attacks }} {{Event="+out_message+"}} {{Fumble Message="+out_fum+"}}"); } else if (out_dam == 0) { out_message = "With a roll of " + at_roll + " has hit " + target_name; sendChat('', "&{template:default} {{name="+char_name+" attacks }} {{Event="+out_message+"}} {{Damage="+out_dam+"}}"); } else { out_message = "With a roll of " + at_roll + " has hit " + target_name; sendChat('', "&{template:default} {{name="+char_name+" attacks }} {{Event="+out_message+"}} {{Damage="+out_dam+"}} {{Primary Crititcal="+out_pri+"}} {{Secondary Damage="+out_sec+"}}"); } }; }); I have put numerous log entries in to locate where the script is failing. This appears to be working fine, however on occasion I get the following error: Your scripts are currently disabled due to an error that was detected. Please make appropriate changes to your scripts and click the "Save Script" button and we'll attempt to start running them again.  More info... For reference, the error message generated was:  TypeError: Cannot read property 'id' of undefined TypeError: Cannot read property 'id' of undefined at apiscript.js:26:46 at eval (eval at <anonymous> (/home/node/d20-api-server/api.js:151:1), <anonymous>:65:16) at Object.publish (eval at <anonymous> (/home/node/d20-api-server/api.js:151:1), <anonymous>:70:8) at /home/node/d20-api-server/api.js:1634:12 at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:560 at hc (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:39:147) at Kd (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:546) at Id.Mb (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:489) at Zd.Ld.Mb (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:94:425) at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:111:400 From the API logs it can be seen that the issue is that the parameter character is not getting populated: "Black Bear - !attackweapon - params: 30,65,0,Black Bear,0,No,-LTAR_ftr_nuLcDP-jwc" "Black Bear - !attackweapon - wpn_name: 30" "Black Bear - !attackweapon - wpn_total: 65" "Black Bear - !attackweapon - modifiers: 0" "Black Bear - !attackweapon - ambush_skill: 0" "Black Bear - !attackweapon - ambush_attack: No" "Black Bear - !attackweapon - token_id: -LTAR_ftr_nuLcDP-jwc" "Black Bear - !attackweapon - target: [object Object]" "Black Bear - !attackweapon - character: undefined" If I use the second token I get the same error however I have the following in the API log: "Black Bear - !attackweapon - params: 30,65,0,Black Bear,0,No,-LTARc0nMqt7MUZ2dkUy" "Black Bear - !attackweapon - wpn_name: 30" "Black Bear - !attackweapon - wpn_total: 65" "Black Bear - !attackweapon - modifiers: 0" "Black Bear - !attackweapon - ambush_skill: 0" "Black Bear - !attackweapon - ambush_attack: No" "Black Bear - !attackweapon - token_id: -LTARc0nMqt7MUZ2dkUy" "Black Bear - !attackweapon - target: [object Object]" "Black Bear - !attackweapon - character: undefined" From what I read because the token id is different for each of the Goblin tokens, I was of the understanding that I could use a single character sheet loaded onto the page several times. Please can someone confirm what I need to do to make this work? p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545}
Update: The issue is not if there are multiple instances of the token, it is when the token has a Default token (i.e. image) associated to it.
1544242364
GiGs
Pro
Sheet Author
API Scripter
Do you have the token's Represents setting correctly set on all tokens? It may be that one of them has been altered.
1544242488
GiGs
Pro
Sheet Author
API Scripter
Woody said: Update: The issue is not if there are multiple instances of the token, it is when the token has a Default token (i.e. image) associated to it. Do you mean the problem occurs every time, if the token has been set as default for the character? You might have multiple different versions of the token on your map.  Maybe you need to set a default token, then delete all existing copies, and then drag the new token onto the map to make as many copies as you need.
1544242787
GiGs
Pro
Sheet Author
API Scripter
In the meantime, here's a suggestion to avoid the sandbox crashes: Change this line var character = getObj('character', target.get('represents')); To this: var character = getObj('character', target.get('represents')); if(!character) {         sendChat("AttackWeapon Error","/w GM Error in Script."); return; }
As soon as I attach a graphic to my character sheet the error occurs. I have tested this with a new image. There are definitely no other copies of the graphic on my page. I am assuming that the link between the graphic and character sheet is not working correctly. As for the multiple copies - this was a red herring - the issue occurs even if there is only one copy on the page.
Thanks for the amendment to the script - I can confirm that that has stopped the sandbox crashes.
Once the graphic is removed the script functions correctly.
Worked out what I was doing wrong. I hadn't linked the graphic to the character sheet properly.
1544249539
GiGs
Pro
Sheet Author
API Scripter
I'm glad you got it sorted.