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

Custom API attempt sometimes works, sometimes doesn't

1711666030

Edited 1711666271
Hello all you lovely people of the internet. I tried editing and customizing an API that I found in a post, and for some reason it doesn't always register that it's working. Here's the code const layerToMove = 'gmlayer'; //map,gmlayer const statusToSet = 'status_dead'; const playerStatusToSet = 'status_skull'; const bolLayerMoveOnDeath = true; //true,false let barToCheck = 'bar1_value'; //bar1_value,bar2_value,bar3_value let acceptedValues = ['bar1_value', 'bar2_value', 'bar3_value']; on('chat:message', function (msg) { if (msg.type == 'api' && msg.content.indexOf('!SetHealthBar') !== -1) { var message = msg.content.split(' ')[1]; if (acceptedValues.includes(message)) { barToCheck = message; log('Set Health bar value changed to ' + message); } } }); on('change:graphic:' + barToCheck, function(obj, prev) { //log('MoveDeadToGMLayer Checking: ' + barToCheck + ' event on object ' + obj.get('name')); if (obj.get('_pageid') == Campaign().get('playerpageid') && obj.get('_subtype') == 'token' && obj.get(barToCheck) < 1 && prev[barToCheck] > 0 ) { var players=obj.get('controlledby').split(/,/); if(obj.get('represents') != '') { var represent = getObj("character", obj.get("represents")); var characters = represent.get('controlledby').split(/,/); if(players[0] == '' && characters[0] == '') { obj.set(statusToSet, true); } else { obj.set(playerStatusToSet,true); sendChat('',obj.get('name') + " is dying!"); } } } if (obj.get('_pageid') == Campaign().get('playerpageid') && obj.get('_subtype') == 'token' && obj.get(barToCheck) <= obj.get(barToCheck.replace("value", "max")) / 2) { obj.set("tint_color", '#ff0000'); } else { obj.set("tint_color", 'transparent'); } if ((obj.get('_pageid') == Campaign().get('playerpageid') && obj.get('_subtype') == 'token' && obj.get(barToCheck) > 0) ) { obj.set(statusToSet, false); obj.set(playerStatusToSet, false); } }); Again, I'm not the one who made the original code, and I'm not sure who originally posted this as I lost the information, but I'd really appreciate any help in getting this to run consistently. Thank you!
So there are a lot more experienced Mod writers and JS people around these forums than me but I was wondering about what you mean when you say "it doesn't always register that it's working". Do you mean that it doesn't always respond to bar changes? Do you mean that the Mod doesn't respond the the chat message to set the health bar? If you are wondering about why it might not be setting the status, a couple things to note are this uses the the Campaign object's playerpageid property. This means the page with the player ribbon. So if you are on a different page than the player ribbon, then this script isn't grabbing tokens on that page. Another thing to note is if you are using another Mod to change the health, like TokenMod or ScriptCards or something like that, then typically API events do not trigger from other Mods. Also usually you will see other Mods  use an `on("ready", function()` statement that contains their other events. Like  TokenMod example . Guessing that is to make sure the API sandbox is up before trying to register event handlers.
1711672047
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Joshua N. said: Also usually you will see other Mods  use an `on("ready", function()` statement that contains their other events. Like  TokenMod example . Guessing that is to make sure the API sandbox is up before trying to register event handlers. More importantly, it limits the scope of all variables to just that script.
1711739657
David M.
Pro
API Scripter
You might want to check out this thread discussing using the Revealing Module Pattern in Roll20 scripts. It includes a generic template that can be modified for your specific script. 
Joshua N. said: If you are wondering about why it might not be setting the status, a couple things to note are this uses the the Campaign object's playerpageid property. This means the page with the player ribbon. So if you are on a different page than the player ribbon, then this script isn't grabbing tokens on that page. Thank you everyone! This turned out to be the exact issue. Thank you all for the help and advice, I really appreciate it a lot!