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

Looking for a smiple script to modify bars

I'm looking for a simple script to modify the values in a selected tokens bars, but my javascript knowledge is non-existent and I couldn't find something to do what I want in the forum. Can someone help me out? I want the script to substract a given number from one of the bars. Let's say bar1 = h(ealth), bar2 = f(atigue) and bar3 = m(ana). Now I want to give a command like "!substract h 4" and the script should substract 4 from the selected tokens bar1.
What should happen if the target tokens bars go above the max value or below zero?
The script doesn't need to have any failsave abilities.
1392724993

Edited 1392727533
Here ya go... Selected Token Macros !alter @{selected|token_id} H ?{Alter Health Bar|0} !alter @{selected|token_id} F ?{Alter Fatigue Bar|0} !alter @{selected|token_id} M ?{Alter Mana Bar|0} Target Token Macros !alter @{target||token_id} H ?{Alter Health Bar|0} !alter @{target||token_id} F ?{Alter Fatigue Bar|0} !alter @{target||token_id} M ?{Alter Mana Bar|0} ... just enter +X or -X to change the bar value. By doing it this way, you only have to have three macros to add and subtract instead of two for each bar to add./subtract individually. on("chat:message", function(msg) { // Exit if not an api command if (msg.type != "api") return; // Get the API Command msg.who = msg.who.replace(" (GM)", ""); msg.content = msg.content.replace("(GM) ", ""); var n = msg.content.split(" "); var command = n[0].toLowerCase(); // Alter the Selected or Targeted Token's Bars // Usage : !alter target bar amount // Example : !alter @{selected|token_id} h -4 // Example : !alter @{target|token_id} m ? if (command == "!alter") { // Define variables... var Target = getObj("graphic", n[1]); var Bar = 0; Bar = (n[2].toLowerCase() == "h") ? 1 : 0; Bar = (n[2].toLowerCase() == "f") ? 2 : Bar; Bar = (n[2].toLowerCase() == "m") ? 3 : Bar; if (Bar == 0) { sendChat("ERROR", "/w " + msg.who + " That is not a valid bar. Please use the first letter as follows: H(ealth), F(atigue), or M(ana)."); return; } var AlterValue = parseInt(n[3]); // Get bar values... var TargetBarCurrent = parseInt(Target.get("bar" + Bar + "_value")); // Set the new value on the token... Target.set("bar" + Bar + "_value", TargetBarCurrent += AlterValue); } });
Great! It's working. Thank you very much!
I did put in a failsafe for bar assignments since you have specific definitions for each bar. Glad it's working for you.
That really reduces a lot of bookkeeping in my campaign since every attack and defense costs the fighter an amount of fatigue and it's tedious to substract it manually after every roll...
Ah. You can definitely fit that into any attack macro. Just replace the ?{Alter Fatigue Bar|0} with the cost of the attack. So for example... if you had an attack called Hamstring and it cost two fatigue, you could use a macro like: /me hamstrings his foe. Hamstring (Fatigue -2) Attack: [[1d4]] Damage: [[1d4]] !alter @{selected|token_id} F -2
Is there a way to automatically detract mana whenever new cards are dealt to the initiative tracker too?
Btw. when I use capital letters as in your example I get an error message: "That is not a valid bar. Please use the first letter as follows: H(ealth), F(atigue), or M(ana)."
Oh doh. I must have removed the toLowerCase part. Lemme fix that.
1392727571

Edited 1392727629
It's fixed now. I added .toLowerCase() to the Bar = (n[2] sections. It should work with upper or lower case now. As for the mana part... I don't know. The card deck and API don't play well together or at all yet, I believe.
But it would be possible if I use rolls for ini instead of cards?
Yeah. Definitely if you used rolls instead of cards.
Ah.. well.. can't make my players life too easy ;)
I can't seem to get the target-token variant to work. I enter "!alter @{target||token_id} H -5" then get to click on a token but nothing happens afterward.
Soory, my bad. It's working now - I had an error in the api console and scripts were stopped.
There seems to be something wrong with the script - I'm getting a lot of stopped apis with this error message provided: "Infinite loop or long running process detected." No other script in the api, when I restart it's working as it should but at some point the api stops and the message returns.
1392971988
Alex L.
Pro
Sheet Author
Petrus A. said: There seems to be something wrong with the script - I'm getting a lot of stopped apis with this error message provided: "Infinite loop or long running process detected." No other script in the api, when I restart it's working as it should but at some point the api stops and the message returns. That's just something the API does some times
Ah.. ok. Thanks!
Yeah, definitely not that script. It only fires when there is a chat message sent. As Alex said... the API just does that sometimes.
1392987985
Alex L.
Pro
Sheet Author
HoneyBadger said: Yeah, definitely not that script. It only fires when there is a chat message sent. As Alex said... the API just does that sometimes. It's because sometimes the server gets a bit slow and so your script takes to long to execute so it stops it i think.
Is there a way to pass a random number such as damage to the !alter I tried to pass a 1d6 and have that subtracted from the health bar but it would error out? Works okay with passing a simple whole number to the health bar.....
1394959947
Alex L.
Pro
Sheet Author
Ajax said: Is there a way to pass a random number such as damage to the !alter I tried to pass a 1d6 and have that subtracted from the health bar but it would error out? Works okay with passing a simple whole number to the health bar..... from the way it is currently code no, it would take some modification to get it to handle inline rolls.
Actually it works just fine with regular rolls. !alter @{target|token_id} h -1d6 will work.
1395000191
Lithl
Pro
Sheet Author
API Scripter
HoneyBadger said: Actually it works just fine with regular rolls. !alter @{target|token_id} h -1d6 will work. That shouldn't work. Your code is calling parseInt(n[3]) , which I believe should result in -1 for the input value -1d6.
Ah, I only did a rough test... and saw the bar move on the token. Hrm. Oh well... I'll update the script later tonight to work with rolls.
New script! This one will accept a dice roll. No square brackets. Addition and subtraction works! !alter @{selected|token_id} h 1d6+4 !alter @{selected|token_id} h -1d6+4 on("chat:message", function(msg) { // Exit if not an api command if (msg.type != "api") return; // Get the API Command msg.who = msg.who.replace(" (GM)", ""); msg.content = msg.content.replace("(GM) ", ""); var n = msg.content.split(" "); var command = n[0].toLowerCase(); // Alter the Selected or Targeted Token's Bars // Usage : !alter target bar amount // Example : !alter @{selected|token_id} h -4 // Example : !alter @{target|token_id} m ? if (command == "!alter") { // Define variables... var Target = getObj("graphic", n[1]); var Bar = 0; Bar = (n[2].toLowerCase() == "h") ? 1 : 0; Bar = (n[2].toLowerCase() == "f") ? 2 : Bar; Bar = (n[2].toLowerCase() == "m") ? 3 : Bar; if (Bar == 0) { sendChat("ERROR", "/w " + msg.who + " That is not a valid bar. Please use the first letter as follows: H(ealth), F(atigue), or M(ana)."); return; } var AlterValue = n[3]; var TargetBarCurrent = parseInt(Target.get("bar" + Bar + "_value")); if (AlterValue.indexOf("d") != -1) { var MinusCheck = AlterValue.charAt(0); AlterValue = (MinusCheck == "-") ? AlterValue.substring(1) : AlterValue; sendChat('', '/r ' + AlterValue, function(outs) { if (MinusCheck != "-") Target.set("bar" + Bar + "_value", TargetBarCurrent += JSON.parse(outs[0].content).total); if (MinusCheck == "-") Target.set("bar" + Bar + "_value", TargetBarCurrent -= JSON.parse(outs[0].content).total); }); } else { Target.set("bar" + Bar + "_value", TargetBarCurrent += parseInt(AlterValue)); } } });
This is fantastic. Thanks HoneyBadger! I'm currently using your script and calling it from a macro thusly: /me calls upon the healing powers of REDACTED. @{selected|token_name} recovers slightly. !alter @{selected|token_id} h 1d8 This works great, but I would very much like to be able to put the value rolled on the 1d8 into chat so that instead of saying "...recovers slightly." It would say "...recovers [5] hit points." What would it take to do that? Again -thank you so much, I really don't want to learn javascript at this point in my life but I can take simple clear examples like yours and tweak them slightly if needed. I learn so much more that way than from any book or class.
Great Script, speeds up combat by a third.... Would work well for healing as well, if there was a way to set the healing not exceed the normal HPs....
There is. I actually have that as part of another script.
Khrain said: This is fantastic. Thanks HoneyBadger! I'm currently using your script and calling it from a macro thusly: /me calls upon the healing powers of REDACTED. @{selected|token_name} recovers slightly. !alter @{selected|token_id} h 1d8 This works great, but I would very much like to be able to put the value rolled on the 1d8 into chat so that instead of saying "...recovers slightly." It would say "...recovers [5] hit points." What would it take to do that? Again -thank you so much, I really don't want to learn javascript at this point in my life but I can take simple clear examples like yours and tweak them slightly if needed. I learn so much more that way than from any book or class. Yeah, I could.. but it would be easier to just add a sendChat with the value.
Ajax said: Great Script, speeds up combat by a third.... Would work well for healing as well, if there was a way to set the healing not exceed the normal HPs.... Ok, I got it set so that you can tell the script to only adjust the bar up to the max value of the bar. I'm working on adding an option to announce how much the bar was changed for.
1395270150

Edited 1395338367
Here's the updated script... Added variables to define the tag used to tell the script which bar to subtract from. These are Bar1Key, Bar2Key, and Bar3Key. In the script below, they are h, f, and m for (H)ealth, (F)atigue, and (M)ana. Added a variable to define the names of the three bars. You must use this format: ["Bar1Name", "Bar2Name", "Bar3Name"] or else it will not work. These are only use if you set ANNOUNCE_CHANGE to true. Added ANNOUNCE_CHANGE to send a message to the chat window to announce which token gained or lost points from which bar. Added PREVENT_OVERMAX to prevent a bar from gaining enough points to go over its max value. If a max value is not set for a bar, this variable has no effect. // USER CONFIGURATION // Set the PREVENT_OVERMAX variable to true to prevent the value of a // token's bar from being greater than its max value. If there is no max // value set, it will not stop the current bar value from increasing. var PREVENT_OVERMAX = true; // Set the ANNOUNCE_CHANGE variable to true to send a message to the chat // window, announcing which token gained or lost points on a bar. var ANNOUNCE_CHANGE = true; // BAR CONFIGURATION - These are used to identify which bar to adjust. The // example below was made for a specific Roll20 user's preferences. These // bar keys must be lowercase, though players can use any case in their // macro since the script converts everything to lower case. var Bar1Key = "h"; // (H)ealth var Bar2Key = "f"; // (F)atigue var Bar3Key = "m"; // (M)ana // BAR NAMES - These names are used if ANNOUNCE_CHANGE is set to true. The // format of the annoucement is: Name gained/lost # BarName. var BarNames = ["Health", "Fatigue", "Mana"]; on("chat:message", function(msg) { // Exit if not an api command if (msg.type != "api") return; // Get the API Command msg.who = msg.who.replace(" (GM)", ""); msg.content = msg.content.replace("(GM) ", ""); var n = msg.content.split(" "); var command = n[0].toLowerCase(); // Alter the Selected or Targeted Token's Bars // Usage : !alter target bar amount // Example : !alter @{selected|token_id} h -4 // Example : !alter @{target|token_id} m ? if (command == "!alter") { // Define variables... var Target = getObj("graphic", n[1]); var Bar = 0; Bar = (n[2].toLowerCase() == Bar1Key) ? 1 : 0; Bar = (n[2].toLowerCase() == Bar2Key) ? 2 : Bar; Bar = (n[2].toLowerCase() == Bar3Key) ? 3 : Bar; if (Bar == 0) { sendChat("ERROR", "/w " + msg.who + " That is not a valid bar. Please use the first letter as follows: H(ealth), F(atigue), or M(ana)."); return; } var AlterValue = n[3]; var TargetBarCurrent = parseInt(Target.get("bar" + Bar + "_value")); var TargetBarMax = parseInt(Target.get("bar" + Bar + "_max")); var StartingValue = TargetBarCurrent; // Check for a + or - sign at the start of the AlterValue... var OperandCheck = AlterValue.charAt(0); AlterValue = (OperandCheck == "-") ? AlterValue.substring(1) : AlterValue; AlterValue = (OperandCheck == "+") ? AlterValue.substring(1) : AlterValue; if (AlterValue.indexOf("d") != -1) { sendChat("", "/r " + AlterValue, function(outs) { AlterValue = parseInt(JSON.parse(outs[0].content).total); if (OperandCheck != "-") { if (PREVENT_OVERMAX == true) { AlterValue = (AlterValue + TargetBarCurrent > TargetBarMax) ? TargetBarMax - TargetBarCurrent : AlterValue; } Target.set("bar" + Bar + "_value", TargetBarCurrent += AlterValue); if (ANNOUNCE_CHANGE) sendChat("", Target.get("name") + " gained " + AlterValue + " " + BarNames[Bar-1] + "."); } else { Target.set("bar" + Bar + "_value", TargetBarCurrent -= AlterValue); if (ANNOUNCE_CHANGE) sendChat("", Target.get("name") + " lost " + AlterValue + " " + BarNames[Bar-1] + "."); } }); } else { if (OperandCheck != "-") { AlterValue = parseInt(AlterValue); if (PREVENT_OVERMAX == true) { AlterValue = (AlterValue + TargetBarCurrent > TargetBarMax) ? TargetBarMax - TargetBarCurrent : AlterValue; } Target.set("bar" + Bar + "_value", TargetBarCurrent += AlterValue); if (ANNOUNCE_CHANGE) sendChat("", Target.get("name") + " gained " + AlterValue + " " + BarNames[Bar-1] + "."); } else { Target.set("bar" + Bar + "_value", TargetBarCurrent -= AlterValue); if (ANNOUNCE_CHANGE) sendChat("", Target.get("name") + " lost " + AlterValue + " " + BarNames[Bar-1] + "."); } } } });
Absolutely Terrific!!!!
Let me know if you have any problems.
1395334826

Edited 1395334853
Ajax
Pro
I did find one glitch, after setting the "report bar change" to false the script still reported the bar change in the chat window, I myself don't see this as a problem however. Let me say again this is an incredible time saver, thanks again HB...
Doh.... I forgot to even put in the announce chat if/else block to stop it from sending to the chat window, lol. I'll get to that tonight.
It's fixed now. Just had to put if (ANNOUNCE_CHANGE) in front of each sendChat message.