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

[Script] Auto Initiative

1371762641

Edited 1384285340
Hey all, I have created an automatic initiative system. This will roll initiative for all selected tokens on the current players map, add a relevant attribute, and then sort it based on their result (highest goes first). There are a couple of globals for you to set: Combat_Begins.statName = new Array ("Dex"); //Stats to be added to roll, commas between values Combat_Begins.rollValue = 20; //rolling 1d20, change if you roll 1dXX Combat_Begins.sendChat = true; //True if you want the chat log to show their results Combat_Begins.includeChars = true; //set false if you want to roll for players To use this, in the array type as many modifiers as you would like. Capitalization does not matter. It will combine them together and roll the die and add the combine modifier to the result. Here is a link to the Gist: <a href="https://gist.github.com/DarokinB/5826648" rel="nofollow">https://gist.github.com/DarokinB/5826648</a>
So if I was to modify this for another system (namely shadowrun) I'd change Combat_Begins.statName = DEX; to Combat_Begins.statName = Initiative; I'm really new to this and not sure is the API can even do that math or if it should be formatted differently, but basically It require no random roll of a single die type is requires the d6&gt;5 rolled by an amount = Init then the value of init to be added to the number of success the first roll generated. So if my Init was 10 i'd roll 10d6&gt;5 + 10 Im not sure how to phrase it in the API as I know very little about JavaScript, I think I do some kind of for loop but the more I think about it the more over my head i realize i am. var dietotal = 0 var hit = 0 for i&lt;= Modifier i++ {&nbsp;&nbsp; randomroll(6) = dietotal &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (dietotal &gt; 4) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hit++&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return; } var init = (hit + Modifier) Something like that? I'm learning this all as I go sorry if this is way noobish.
I've added this to my campaign and I've put all the player tokens on the active page but when I try and run it via type !CombatBegins nothing happens. I've made sure all tokens that are on the page have a DEX stat configured. Am I doing something wrong?
nope the same thing happened to me too. but then, my other scripts that checked the init tracker were crashing the api sandbox. so i disabled them, and then THIS script worked. so i', guessing the others need to update their scripts. (also, the script does NOT automatically call up the init tracker to your screen - though that WOULD be useful to a lazy person to me - so check make sure they aren't in teh init tracker and its just closed.)
Check for anything still doing .get("_turnorder") and change it to .get("turnorder").&nbsp; Also, nice script, Josh!
kewl. easy fix, thank you
Also, watch for: on("change:campaign:_turnorder") ... I had that error as illegal. I will be updating my scripts tonight.
Great script. &nbsp;Quick Q - is it an easy edit to have the script use a defined ability, instead of an attribute, to calculate the initiative scores?
Josh I'm worring that I will need help once more :/ I have the same problem as Liam. After changing DEX to Initiative, !CombatBegins does nothing but clearing the initiative list. I deleted all other scripts I used but that didnt help.
Do all your characters have the ability "Initiative"? If they do not, than it will error.
I fixed it so that if your attribute does not exist, it ignores it.
some suggestions? search graphic by those on the object layer, so it leaves out graphics you might have on the DM layer or the map layer.&nbsp; then perhaps further narrow down selecting by those that have a currently active bar1 above 0? one thing you need to allow for is tokens that have no attached character sheet - I would remedy this by searching for a character sheet with a name the same as the token, and pulls stats from that one. this would allow for all goblins on the page to pull their initiative modifier from the goblin entry, without the sheet being tied to the tokens on the page. also, have it add any tokens added to the object layer during a fight to the initiative order automatically, if possible. your thoughts on these suggestions?
Hi Michael. I thought about doing that, but the problem exists when you are doing a dungeon with multiple rooms and encounters in a map it will add all of them into a single encounter. I decided the only way for it to work is to only add the players and add them. One thing I thought about was keeping any non-players on the initiative tracker and rolling for them. The problem there is there are no mods tracked for them since they are not connected to a player, so you will have to add modifiers to it manually (For instance, if a will'o'wisp you are using has a +9 mod, its not tracked, so you would have to edit their roll by +9 and reorder it).&nbsp; Both these proved to cumbersome to overcome, and didn't lessen the amount of work on the DM, so I did not spend time on this.
Yes only the PCs have the Initiative-Attribute. But it does not work even after your changes :/
Are you putting values in the left or right hand block?
EVERY sheet I create has an Initmod and a Dexterity attribute - even the monsters. its all the in add new character script I run. And for monsters, its definitely in there. Every monster I assign has a page on the character sheet - most are retired, but they have them. including a token assigned to them so I can just pull up the monster and drag them onto the map, ready to go. Having your script read the name, search for the associated sheet for stats (dexterity and initmod) and apply them to the monsters in question GREATLY reduces the work involved in setting up a combat. for Pre-arranged fights I have a lot of tokens on the GM layer- and they ALL have the same stat sheets as characters, even if there bars are not linked to the attributes in question. &nbsp;understand that for you, it makes no difference. For me, and I'm sure for others that do something similar, it does. I'm just asking if you would write that into your code, as it would make it more versatile and allow for more people (like myself) to use your initiative roller. which I would prefer to do, because quite simply you were interested enough in the idea to write the first script for it, and because you cared enough to present the Script to the community. for that, I thank you, by the way. and yes, it makes a difference, because your script only runs on pages i have set up with nothing else on them. I haven't pinpointed exactly what breaks it on my campaign pages yet, which is why i'm not posting about those problems yet (I dislike just moaning its broken - fix it! I like to have something I can reply with to help it get fixed)
Its an Attribute on the left side. And there in the left one of the two fields. I now made a new page with just one complete new PC token using only the Initiative Attribute. Again its not working.
I would also like to put it out there that I might sound like a picky b*****d, but i really do appreciate your work - I also use your status tracker (with a few changes to fit my game) you wrote. and I'm sure a few others. an't remember who wrote all my scripts by memory sadly.
Michael H. said: EVERY sheet I create has an Initmod and a Dexterity attribute - even the monsters. its all the in add new character script I run. And for monsters, its definitely in there. Every monster I assign has a page on the character sheet - most are retired, but they have them. including a token assigned to them so I can just pull up the monster and drag them onto the map, ready to go. Having your script read the name, search for the associated sheet for stats (dexterity and initmod) and apply them to the monsters in question GREATLY reduces the work involved in setting up a combat. for Pre-arranged fights I have a lot of tokens on the GM layer- and they ALL have the same stat sheets as characters, even if there bars are not linked to the attributes in question. &nbsp;understand that for you, it makes no difference. For me, and I'm sure for others that do something similar, it does. I'm just asking if you would write that into your code, as it would make it more versatile and allow for more people (like myself) to use your initiative roller. which I would prefer to do, because quite simply you were interested enough in the idea to write the first script for it, and because you cared enough to present the Script to the community. for that, I thank you, by the way. and yes, it makes a difference, because your script only runs on pages i have set up with nothing else on them. I haven't pinpointed exactly what breaks it on my campaign pages yet, which is why i'm not posting about those problems yet (I dislike just moaning its broken - fix it! I like to have something I can reply with to help it get fixed) Man, thats a lot of work. I keep the monster stats on my books in my lap/PDFs etc. I never thought it would be worth inserting all that information for a creature that lasts a few rounds in combat. Kudos to you for all the work in your prep! Thinking about it, it may be a good idea to insert all the monsters in the game so you can just pull them in as you use them. Its an interesting idea. I will play around with this and see what I can do.
Andi said: Its an Attribute on the left side. And there in the left one of the two fields. I now made a new page with just one complete new PC token using only the Initiative Attribute. Again its not working. Capitalization can be an issue as well. DEX, Dex, and dex are all different. Does that help? Also, load the api script show an error at all?
Oh MY GoD! THAT would be MASSIVE undertaking! every monster in the game is beyond even my renal attentiveness. I DO make a sheet for each monster I use though, or plan on using, just as I do NPC's. then I archive it, in case I want it again. with the automatic character sheet script for 3.5 its easy - just copy out relevant bits into it, link a token to it, and fill in the HP bar manually - i usually select HP then put it back onto none - it keeps the stats that way without actually changing the values on the character sheet. then I select as default token, and the rest is simple. Just drag and drop (Tip: keep the tokens on the GM layer when you select them - when you place them on the map they will appear on the GM layer and you can rearrange to taste while the players are talking, then drop them in on them all at once) All those generic goblins on the map are actually linked to one sheet with relevant macros sorted (attacks, saves, etc, just like a character) but each with their own HP total and whatnot. each time i throw a spanner in the works and add a monster I didn't plan on having in the fight, i just add new character, copy in stats for it, and link a token set up the way I want it. Easy. and really, its less than a minute of work to add a new base monster, the abilities and attributes are already there on the sheet to fill out. Just copy and paste any special notes into the tokens bio (special attacks, modifiers or anything else you want - i just copy the entire relevant sections from the monsters stat blocks into it myself)
The one thing that would be great would be if we could make encounter "groups" and manipulate them as a group. Meaning, make a group of goblins "encounter 1" and then we could just make encounter 1 only join the initiative, become visible, etc.
hmmmmm easy to do. one attribute added that returns a group value . harder to code though. well, more complex. been contemplating that for my players (ie, an attribute that groups them as pcs together) haven't needed to yet, but if you are going to hard-code a check for it into your script, then expanding it to include groups of npcs and monsters IS the next logical step - and if its a global var, it can be hooked into by any other script you write that needs to know where a character belongs (I was thinking of working one that allowed you to buff 'allies' or debuff 'enemies' - this is just an expansion of that really.) so - an attribute called group, with a string indentifier? so for PC group you could simply call it PC. prearranged groups of NPC's all set to be included in the combatbegins command line based on group value? or have you some easier method in mind?
Well, if bar3 is not used, we could assign a group number to that. This way, if you have a single 'character' used multiple times, you can still have them be used in different groups. For instance, if you use a lot of zombies, you can add one to the character slot, keep pulling them in, assign HPs as you do, and in bar 3 do the different groups. That could work.
good point. Hadn't thought that through the entire way. hmmmm. It seems to be the best way. I frequently use bar2 for spellpoints, lay on hands, things the character in question keeps track of like that. i tend to use bar3 for power attack or combat expertise, depending on the character. I can change that to an API command though, once I figure out how to properly assign such things.
Josh said: Andi said: Its an Attribute on the left side. And there in the left one of the two fields. I now made a new page with just one complete new PC token using only the Initiative Attribute. Again its not working. Capitalization can be an issue as well. DEX, Dex, and dex are all different. Does that help? Also, load the api script show an error at all? Yes i checked the capitalization several times before i start posting here. Also there are no helpful messages at all in the API log. Ok maybee i just did something stupid setting this script up. So i will, like in your teleport-post, name all steps I did. 1. as usual copy &amp; paste the script from here into a new script tab. 2. changing Combat_Begins.statName = "DEX" into Combat_Begins.statName = "Initiative" 3. making a new character in the journal and giving him an "Initiative" attribute with the value 2. 4.1. placing a new token on a blank map and linking the token to the charakter ("Represents character") 4.2. Also i tried setting the second bar as the initiative bar but that didn't change the outcome. 5. using !CombatBegins to start the script. Noting happens. When I had the token in the initiative-list before starting the script, the list only becomes blank. Thank you again for your effort to make scripts, charing them with us and help us making it work. I really appreciate that :)
Ok, so I did an update to the script. First, I added a case insensitive to the search to see if capitalization is an issue. I also removed the DM test for the moment, I will add something generic later, to see if people get the command running. When you type !CombatBegins (capitilization here is VITAL!), it will send a chat line : "Running..." If you do not see it, the API has crashed and is not running the script (or any others). Lets see if this helps solve the problem.
Yes with the new version i get the "Running..." chat line but again the token does not appear in the Initiative table. To show you my settings, I uploaded a screenshot of my roll20 table right after starting the script. Maybee you can see something that I did wrong setting up the token. <a href="http://www.imagesup.net/di-13137210987010.png" rel="nofollow">http://www.imagesup.net/di-13137210987010.png</a>
What are all the settings you are using? What is the dice roll? Combat_Begins.fourth_ed = false; //If using 4th edition, it takes 1/2 'Level' and adds it for initiative Combat_Begins.statName = "Initiative"; //Stat to be added to roll Combat_Begins.rollValue = 20; //rolling 1d20, change if you roll 1dX
I hope I understood your question right. I am playing pathfinder so I didn't change anything in the dice options. And in your script I only changed the "Combat_Begins.statName" from DEX to Initiative. Of course I was not doing that in the newest version because you already changed that by yourself. So I only copy and pasted it without changing anything.
Tell you what - ill start trying to break this thing down in chuncks whdn I get home tonight. I can get it to work with a ank map but havent found what breaks it just yet ill focus on that tonight instead of trolling the forums
Just as a test, set Initiative to 5 / 5 (so there is a value on the right). I didn't use it in my script, but I can't explain this bug you are having.
ok, ive been playing with this for a few hours, haven't managed to break it on purpose yet, but going to keep trying. it works fine on a blank page, though it wont work on one of my maps, just haven't figured out why yet.
No. Doesnt help. Well I guess i will have to wait for an API update and hope for it to work then. But thanks for your patience :)
wait just read back.&nbsp; this script only adds tokens that have character pages assigned to them. you have linked a character sheet to a token, from what I understand.&nbsp; it has an attribute called Initiative all correct? the only way I was able to break the script in the same manner was when i had a spacebar in the attribute name, at the end.&nbsp; i am just wondering if something similar might be your problem, as the script does work - check and double check your attribute name for something that isn't obvious.
Two observations: 1) Since it wasn't specifically asked, Andi you have the Player Ribbon on the same page as the one you want to the script to work on, right? It's set up to only look at graphics on the active player page. 2) I would add some log statements around line 19 listing the number of characters it's finding to see if that's the issue. If that's not it, maybe log around line 30 to see if it's finding the attribute successfully. (Sorry if any/all of this has been suggested already, I only had time to quickly skim the thread).
Ok, so I have tested on all 20 of my maps in my current campaign. I came across two problems which is an API problem and not my code. First, if a token was linked to a character, but then the character was deleted (we had a player leave, never deleted the token because we don't use that map), it causes an error. To fix this, if you pull up the token, select none as it represents, and save, it fixes the problem. The second is nothing I can do about. The problem is that it adds them to the turnorder, but it does not update the initiative tracker. To solve this, I click on the arrow in the turn order to move to the next person several times, and then type !CombatBegins again, and it works properly. I don't know why this is happening. As another fix, if you close and reopen the initiative tracker, it updates properly.
Riley, I have actually updated it so it does the following: 1) Only looks on the objects layer 2) Logs the character and their resulting initiative In my logs, I get rolls every time, but sometimes the initiative tracker does not update itself. Is there a way to force a refresh?
Hmmm..that is odd. The only reason I can think of it wouldn't be updating is if the value's not actually changing... For your other problem about the token being linked to a character that doesn't exist, you should add the following line to your code around line 26: if(!currChar) return true; Right now you're assuming that you will always find a valid Character, and that may not happen for a variety of reasons. Adding that line will just skip that token if there is no valid Character.
@ Riley: Yes its the active player map I use. Ok I finally got it to work. I made a brand new campaign (damn I really could have done this earlier -.-') just for testing and in this new one it works perfectly. I just don't get it to work in my existing campaign which I copied from the normal server. So something in this campaign seems to disturb the script. I guess this element must be something global because the map I tested the script on was new. Also it is not another script because for testing i deleted the only other one I was using (automatic status marker). I mention this things because I was already wondering if it could be my browser or its setting that maybee causes problems with the API.
don't know if this is cool, Josh, but I modified your code to work with DnD 3.5 or another system that has two possible modifers to it; I basically put in another modifier (Initmod on my character sheets, but added as the same format in your code so it could be set manually in the options) that can be applied to allow more than dex to be added to initiative. (one guy on my campaign always takes improved initiative. ALWAYS). I also added a few lines to print out a chat message with the math for the players to see what they rolled and what modified it for them. is it ok to post in here? Its not my script, so yea, don't want to make a new post for it, its really just a slightly modified version of yours that i'd like to make available to the community.&nbsp; did I mention that I put in a few other extras? Added a check to see if a token has less than zero HP it isn't included in the initiative rolls.&nbsp; what else?&nbsp; oh, a setting to allow a chat message to be sent to chat to show players what they rolled and what the modifiers where. dont think there was anything else......
Of course it's cool, have at it!
1372635502

Edited 1375979771
Nice!
it works right? been editing a lot of scripts i think this one was complete&nbsp;
I'm new to scripting in Roll20...if I copy and paste this script into my list of scripts, and then try to go to my chat log and type !Combat_Begins but nothing happens. &nbsp;What do I need to do to format the message as an API message? &nbsp; msg.type == "api" I've created a character with both a Dexterity and an Initmod attribute, but I don't even think its getting there since I don't see the &lt;rolls for initiative&gt; message. &nbsp;Halp!
Its !CombatBegins, no underscore
Haha, I am an idiot. &nbsp;Thanks so much, works great!
This script is amazing, although I had to modify it so it receives the dexterity and then calculates the modifier, and I believe the variable to check for fourth edition is not being used, so I added that as well. Also added a simple chat message to tell which player how much they rolled (just a little modification from the log to separate the init stat from the roll). Why the +1 during the roll, by the way? From what I understand, the randomizer function already does from 1 to max.
Oh haha I need to fix that. There was an error with the randomizer that it was giving 0-20 instead of 1-20. They have patched it and I had not updated my code yet
Might be a dumb question, but from where does it get the characters level? EDIT: I made a new variable that looked for an attribute called Level, then if&nbsp;Combat_Begins.fourth_ed = true it got the number from the attribute and 1/2 it and added the number to the total.&nbsp; it did work, but im still looking for a original way in doing it, since the variable Combat_Begins.fourth_ed is mentioned in the code. maby i am using an outdated version.&nbsp;