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] API Code Failing - Not sure why

I am trying to recreate complex paper based roll tables using API scripts which will be called from macros attached to the character sheet. The code I am using 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("!roll1 ") !== -1) { var numdice = msg.content.replace("!roll1 ", ""); var splited = numdice.split(' '); sendChat(msg.who, splited[0]); sendChat(msg.who, splited[1]); sendChat(msg.who, splited[2]); var damage = AT1(splited[0], splited[2]); sendChat(msg.who, damage); }; }); // --------------------------------- function AT_weapon_stats(weapon) { var fumble=0; var pri_crit=0; var sec_crit=0; // Calculate Fumble/Critical Chances/Types if (weapon == 1) {fumble=3; pri_crit=2; sec_crit=0;} else if (weapon == 2) {fumble=1; pri_crit=3; sec_crit=0;} else if (weapon == 3) {fumble=4; pri_crit=2; sec_crit=0;} else if (weapon == 4) {fumble=4; pri_crit=2; sec_crit=0;} else if (weapon == 5) {fumble=3; pri_crit=2; sec_crit=0;} else if (weapon == 6) {fumble=4; pri_crit=1; sec_crit=0;} else if (weapon == 7) {fumble=3; pri_crit=1; sec_crit=0;} else if (weapon == 8) {fumble=8; pri_crit=1; sec_crit=3;} else if (weapon == 9) {fumble=6; pri_crit=5; sec_crit=0;} else if (weapon == 10) {fumble=4; pri_crit=1; sec_crit=0;} else if (weapon == 11) {fumble=6; pri_crit=5; sec_crit=2;} else if (weapon == 12) {fumble=4; pri_crit=3; sec_crit=0;} else if (weapon == 13) {fumble=5; pri_crit=3; sec_crit=2;} else if (weapon == 14) {fumble=75; pri_crit=2; sec_crit=1;} else if (weapon == 15) {fumble=8; pri_crit=1; sec_crit=3;} else if (weapon == 16) {fumble=3; pri_crit=1; sec_crit=0;} else if (weapon == 17) {fumble=5; pri_crit=2; sec_crit=1;} else if (weapon == 18) {fumble=3; pri_crit=5; sec_crit=1;} else if (weapon == 19) {fumble=5; pri_crit=3; sec_crit=0;} else if (weapon == 20) {fumble=7; pri_crit=3; sec_crit=0;} else if (weapon == 21) {fumble=4; pri_crit=3; sec_crit=0;} else if (weapon == 22) {fumble=5; pri_crit=3; sec_crit=0;} else if (weapon == 23) {fumble=5; pri_crit=1; sec_crit=0;} else if (weapon == 24) {fumble=4; pri_crit=2; sec_crit=0;} else if (weapon == 25) {fumble=6; pri_crit=2; sec_crit=0;} var return_value = [fumble, pri_crit, sec_crit]; return return_value; }; function AT1(weapon, target_armour) { var weapon_details = AT_weapon_stats(weapon); var return_value; let fumble=weapon_details[0]; roll_val = Math.floor(Math.random() * Math.floor(100)); if (roll_val < fumble) {return_value="F";} else {return_value = roll_val;} sendChat('API',"weapon_details[0]: " + weapon_details[0]) sendChat('API',"weapon_details[1]: " + weapon_details[1]) sendChat('API',"weapon_details[2]: " + weapon_details[2]) sendChat('API',"roll_val: " + roll_val) sendChat('API',"weapon: " + weapon) sendChat('API',"target_armour: " + target_armour) sendChat('API',"return_value: " + return_value) return roll_val; }; I am calling the function using the following command which is located inside of a fieldset <td class="sheet-table-data-center-stats"><textarea rows="1" style="width: 45px; height: 19px; margin-bottom: 0;" name="attr_wpn-attack-macro">!roll1 @{wpn_name} @{wpn_total} @{target|main_armour}</textarea></textarea><td> If line 11 (sendChat(msg.who, damage);) is uncommented I get the following error in the API Output Console: 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: op.content.replace is not a function TypeError: op.content.replace is not a function at Object.d20.textchat.doChatInput (eval at <anonymous> (/home/node/d20-api-server/api.js:155:1), <anonymous>:368:29) at sendChat (/home/node/d20-api-server/api.js:1788:16) at apiscript.js:11:5 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) If line 11 is commented out all sendChat calls are successfully made. Please can you advise why line 11 is failing and what I need to do to rectify?
For info in case its relevant. When line 11 is un-commented the only successful sendChat is the first one.
1543055529

Edited 1543070455
Bast L.
API Scripter
Try changing damage on that line to String(damage) I think sendChat doesn't like non-strings. Edit read wrong function. Edit more: though it looks like you're sending a number when you return "return_val", so I think it might still fix it. Testing now: Yeah, the "return_value: " + return_value works because when you add them it turns it into a string, I guess, but later when you're just sending return_val, it breaks.
1543057665
GiGs
Pro
Sheet Author
API Scripter
As an aside, look up the special roll20 api function  randomInteger function I think this  roll_val = Math.floor(Math.random() * Math.floor(100)); should actually be: roll_val = Math.ceil(Math.random() * 100); But with randomInteger it's even simpler roll_val = randomInteger(100);
1543070217
Finderski
Pro
Sheet Author
Compendium Curator
G G said: As an aside, look up the special roll20 api function  randomInteger function I think this  roll_val = Math.floor(Math.random() * Math.floor(100)); should actually be: roll_val = Math.ceil(Math.random() * 100); But with randomInteger it's even simpler roll_val = randomInteger(100); And, if I remember correctly, randomInteger uses the Quantum Roller
1543070477
GiGs
Pro
Sheet Author
API Scripter
It doesn't use quantum roller, but it does use a more robust RNG than javascript's Math.floor. It uses the RNG that roll20 fallsback on when quantum isn't working (my guess is, it's the system they used before quantum roll was introduced).
Cheers ... will continue to persevere with it - and will try converting to a string to see if that is the cause of the issue.