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 .
×

[Help] Turn Order Movement Restriction Script

Hi guys, Could someone please give me a hand with a small script I've been working on? I'm not fantastic with JavaScript or JSON so my code is probably horribly written so please be nice lol. What I want it to do: When I bump the turn order to the next person, only that player should be able to move their tokens. When I say move their tokens, I don't know how (if even possible) to stop players from being able to move their tokens without removing their control over said tokens, so I just put the tokens back to where they were before they moved them. My Code: on("change:campaign", function() {     try     {         var turnOrder = JSON.parse(Campaign().get("turnorder"));         var token = getObj("graphic", turnOrder[0].id); //gets the player's token         var character = getObj("character", token.get("represents")); //gets the player's character         var player = getObj("player", character.get("inplayerjournals")); //gets the player reference         var objs = findObjs(         {             _pageid: Campaign().get("playerpageid"),             _type: "graphic"         });         on("change:graphic", function(obj)         {             var char = getObj("character", obj.get("represents"));             if(char.get("inplayerjournals") !== player.get("_id"))             {                 log("char: " + char.get("name"));                 log("player: " + player.get("_displayname"));                 _.each(objs, function(tokens)                 {                     if(obj.get("_id") === tokens.get("_id"))                     {                         var lastMove = obj.get("lastmove").split(",");                         obj.set("left", parseFloat(lastMove[0]));                         obj.set("top", parseFloat(lastMove[1]));                         obj.set("rotation", tokens.get("rotation"));                     }                 });             }         });     }     catch(err)     {         log("turnOrderRestriction.js encountered an error: " + err.message);     } }); Issue: When I bump to the next person (for the first time and the second time only) the script works perfectly. Under the listener for graphic changes, I have added:  log("char: " + char.get("name")); and log("player: " + player.get("_displayname")); to try and debug this issue. The issue is that after the second time that I bump the turn order, the listener gets set off n-1 times. So when I bump it the 3rd time, it runs twice, when I run it the 4th time, it runs 3 times, etc... My problem is that the log indicates that the first time it runs, all the values are correct, but on the duplicate runs the variables are no longer correct and I can't figure out why. Any thoughts anyone?
You don't need to watch the turn order. Use on("change:graphic") instead and see if the token that moved is at the top of the turn order or if it's an npc and should be ignored. 
Worked perfectly, thanks ^_^
Just realised it might actually help others if I posted the finished working script lol. I've tried to make it more efficient and I realise now that you can specify attributes, moreso than just the object type in the listener. With the initial script, it would be triggered if you changed anything about the token, even their health, light emission, line of sight, etc.. So this new script acommodates for all that and I'm sure that 99% of all of you would have thought of this before I did but oh well here it is lol. Also I have used the exception catchers to handle if the turn order list is empty. I couldn't work out a way to stop it from throwing an exception because it kept returning the turn order list as undefined. on("change:graphic:left", function(obj) {     try     {         var turnOrder = JSON.parse(Campaign().get("turnorder"));         var token = getObj("graphic", turnOrder[0].id); //gets the player's token         var character = getObj("character", token.get("represents")); //gets the player's character         var player = getObj("player", character.get("inplayerjournals")); //gets the player reference         var char = getObj("character", obj.get("represents"));         if(char.get("inplayerjournals") !== player.get("_id"))         {             var lastMove = obj.get("lastmove").split(",");             obj.set("left", parseFloat(lastMove[0]));         }     }     catch(err)     {         var lastMove = obj.get("lastmove").split(",");         obj.set("left", parseFloat(lastMove[0]));     } }); on("change:graphic:top", function(obj) {     try     {         var turnOrder = JSON.parse(Campaign().get("turnorder"));         var token = getObj("graphic", turnOrder[0].id); //gets the player's token         var character = getObj("character", token.get("represents")); //gets the player's character         var player = getObj("player", character.get("inplayerjournals")); //gets the player reference         var char = getObj("character", obj.get("represents"));         if(char.get("inplayerjournals") !== player.get("_id"))         {             var lastMove = obj.get("lastmove").split(",");             obj.set("top", parseFloat(lastMove[1]));         }     }     catch(err)     {         var lastMove = obj.get("lastmove").split(",");         obj.set("top", parseFloat(lastMove[1]));     } });