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?