
For some reason the act of testing a variable is changing it's value in an imperceptible way. I have this statement "var init_roll = Number(die_value) + Number(initval);" Number(intval) returns the correct value when tested, but the above statement disregards the value of intval and instead sets init_roll to the value of die_value. initval is set by getting the property current from the variable attribute. The above statement will work as expected if I do not test the value of attribute in an earlier command. If it helps, this script will go through the initiative list and reroll each value. It worked great until I noticed that some icons didn't have the initiative property set which would cause the script to crash. I am trying to write an exception to handle this, and the exception causes the script to treat all initiative modifiers as zero. Can someone help me figure out a good work around? Has anyone had this issue before? Here is my code: const TestStuff = (() => { let version = '0.0.1', lastUpdate = 1576528439, schemaVersion = 0.1, debugVariable = function(target_var) { sendChat('',JSON.stringify (target_var)); }, rerollInitiative = function() { var newturnorder = []; var turnorder; var gmturns = 0; var current_round = 1; if(Campaign().get("turnorder") == "") turnorder = []; //NOTE: We check to make sure that the turnorder isn't just an empty string first. If it is treat it like an empty array. else turnorder = JSON.parse(Campaign().get("turnorder")); for (let i = 0; i < turnorder.length; i++) { var token = findObjs({_type: "graphic", id: turnorder[i].id})[0]; if (token != null) { var initval = 0; var layer = token.get("layer"); var characterid = token.get("represents"); var attribute = findObjs({type: 'attribute', characterid: characterid, name: 'initiative'}, {caseInsensitive: true})[0]; if (attribute == undefined) { // **** THIS IF STATEMENT BREAKS EVERYTHING //attribute = findObjs({type: 'attribute', characterid: characterid, name: 'CGen_Init'}, {caseInsensitive: true})[0]; } intval = Number(attribute.get('current')); debugVariable(intval); var die_value = Math.floor((Math.random() * 20) + 1); //debugVariable(die_value); var init_roll = Number(die_value) + Number(initval); // ****** HERE IS WHERE LOGIC BREAKS DOWN debugVariable(init_roll); debugVariable('next'); if (layer == "gmlayer") { gmturns = gmturns + 1; newturnorder.push({ id: "-1", pr: init_roll, custom: "GM" }); } turnorder[i].pr = init_roll; newturnorder.push(turnorder[i]); } } Campaign().set("turnorder", JSON.stringify(newturnorder.sort(function(a, b){return b.pr-a.pr}))); }, handleInput = function(msg_orig) { let args, nl, longtext, msg = _.clone(msg_orig); if ( (msg.type !== "api") || (!playerIsGM(msg.playerid)) ) { return; } args = msg.content.split(/\s/); switch(args.shift()) { case '!test-stuff': rerollInitiative(); break; } }, registerEventHandlers = function() { on('chat:message', handleInput); }; return { RegisterEventHandlers: registerEventHandlers }; })(); on('ready',function() { 'use strict'; TestStuff.RegisterEventHandlers(); });