Yeah, this is what I thought, but it looks like I'm getting bitten by the other side of the problem I was having before. I stuck a log statement into the graphic:added handler and dumped the new token object: {"_id":"-Jz88vX15Gaizcm0lUUE","_pageid":"-JytiX0nC2cGZArNwphy","left":977,"top":154,"width":70,"height":70,"rotation":0,"layer":"objects","isdrawing":false,"flipv":false,"fliph":false,"imgsrc":"<a href="https://s3.amazonaws.com/files.d20.io/images/11074701/D2AqoqE5-Pm7PkVIjzWJZw/thumb.png?1437928650","name":"","gmnotes":"","controlledby":"","bar1_value":"","bar1_max":"","bar1_link":"","bar2_value":"","bar2_max":"","bar2_link":"","bar3_value":"","bar3_max":"","bar3_link":"","represents":"-Jz6vF6xvDberySTZm1r","aura1_radius":"","aura1_color":"#FFFF99","aura1_square":false,"aura2_radius":"","aura2_color":"#59E594","aura2_square":false,"tint_color":"transparent","statusmarkers":"","showname":false,"showplayers_name":false,"showplayers_bar1":false,"showplayers_bar2":false,"showplayers_bar3":false,"showplayers_aura1":false,"showplayers_aura2":false,"playersedit_name":true,"playersedit_bar1":true,"playersedit_bar2":true,"playersedit_bar3":true,"playersedit_aura1":true,"playersedit_aura2":true,"light_radius":"","light_dimradius":"","light_otherplayers":false,"light_hassight":false,"light_angle":"","light_losangle":"","light_multiplier":1,"sides":"","currentSide":0,"lastmove":"","_type":"graphic","_subtype":"token","_cardid" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/11074701/D2AqoqE5-Pm7PkVIjzWJZw/thumb.png?1437928650","name":"","gmnotes":"","controlledby":"","bar1_value":"","bar1_max":"","bar1_link":"","bar2_value":"","bar2_max":"","bar2_link":"","bar3_value":"","bar3_max":"","bar3_link":"","represents":"-Jz6vF6xvDberySTZm1r","aura1_radius":"","aura1_color":"#FFFF99","aura1_square":false,"aura2_radius":"","aura2_color":"#59E594","aura2_square":false,"tint_color":"transparent","statusmarkers":"","showname":false,"showplayers_name":false,"showplayers_bar1":false,"showplayers_bar2":false,"showplayers_bar3":false,"showplayers_aura1":false,"showplayers_aura2":false,"playersedit_name":true,"playersedit_bar1":true,"playersedit_bar2":true,"playersedit_bar3":true,"playersedit_aura1":true,"playersedit_aura2":true,"light_radius":"","light_dimradius":"","light_otherplayers":false,"light_hassight":false,"light_angle":"","light_losangle":"","light_multiplier":1,"sides":"","currentSide":0,"lastmove":"","_type":"graphic","_subtype":"token","_cardid</a>":""} I've also got a simple api script that dumps the selected token when requested. I ran this immediately after the token had been added to the canvas, and got this: {"_id":"-Jz88vX15Gaizcm0lUUE","_pageid":"-JytiX0nC2cGZArNwphy","left":977,"top":154,"width":70,"height":70,"rotation":0,"layer":"objects","isdrawing":false,"flipv":false,"fliph":false,"imgsrc":"<a href="https://s3.amazonaws.com/files.d20.io/images/11074701/D2AqoqE5-Pm7PkVIjzWJZw/thumb.png?1437928650","name":"Melia" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/11074701/D2AqoqE5-Pm7PkVIjzWJZw/thumb.png?1437928650","name":"Melia</a> Moonstaff","gmnotes":"","controlledby":"","bar1_value":26,"bar1_max":26,"bar1_link":"-Jz79UPtiMOkePluBCvL","bar2_value":"","bar2_max":"","bar2_link":"","bar3_value":"","bar3_max":"","bar3_link":"","represents":"-Jz6vF6xvDberySTZm1r","aura1_radius":"","aura1_color":"#FFFF99","aura1_square":false,"aura2_radius":"","aura2_color":"#59E594","aura2_square":false,"tint_color":"transparent","statusmarkers":"","showname":false,"showplayers_name":false,"showplayers_bar1":false,"showplayers_bar2":false,"showplayers_bar3":false,"showplayers_aura1":false,"showplayers_aura2":false,"playersedit_name":true,"playersedit_bar1":true,"playersedit_bar2":true,"playersedit_bar3":true,"playersedit_aura1":true,"playersedit_aura2":true,"light_radius":"","light_dimradius":"","light_otherplayers":false,"light_hassight":false,"light_angle":"","light_losangle":"","light_multiplier":1,"sides":"","currentSide":0,"lastmove":"785,150","_type":"graphic","_subtype":"token","_cardid":""} As you can see, it's the same token, but when it first appeared (as supplied to the HP rolling functions) it was pretty much bare apart from the "represents" value and the position. Shortly afterwards it now has a name and bar details that it didn't have the first time round. This causes two problems: First time around the script doesn't know that the token's HP bar is linked, so it runs HP rolling when it shouldn't It seems like *sometimes* when the script writes to the token to set HP, this gets overwritten immediately afterwards by the value from the character sheet at the point that the token gets "filled out", and *sometimes* it happens the other way round. Since this looks like a race condition, I stuck a setTimeout in the add:graphic handler as follows: on('add:graphic', function(obj) {
log(obj);
setTimeout(function() {
log(obj);
shaped.rollTokenHp(obj);
}, 1000); Sure enough, the "obj" changes between the two log calls - all the bar details get filled in. Putting the timeout in there also means that the code now behaves correctly - no rolls for linked bars, reliable rolls for non-linked ones. Have you ever seen anything like this before? I can't see anything else in my other scripts that could be causing this, and, from the behaviour, it seems most likely to be a feature of how Roll20 itself is handling the token. Is it a bug? At the very least it seems unhelpful behaviour.... Or have I got this completely wrong? Cheers, Lucian