Skip to content

Instantly share code, notes, and snippets.

@Bastlifa
Created September 14, 2017 02:47
Show Gist options
  • Save Bastlifa/958c31e6e46aafe1a5bcbbd5fe3355cd to your computer and use it in GitHub Desktop.
Save Bastlifa/958c31e6e46aafe1a5bcbbd5fe3355cd to your computer and use it in GitHub Desktop.
Token control script for roll20
/* Token control script
make a macro called TokenControl,
the body should read:
!TokenControl @{target|token_id} @{selected|token_id}
To use this, select a token that you want to alter the control of
then click the macro from your bar
then target a token of a player. I think the same token could work.
This will, hopefully, alter who controlls the selected token.
If the targeted token's player had control, it will remove it
if it did not, it will add it. */
on("ready", function() {
"use strict";
function TokenControl(targetID, msg, selectedID)
{
var tokenChar = getObj('character', getObj('graphic', selectedID).get("represents"));
//find the token with target id, then the character it represents, then the first player who controlls that token.
var player = getObj('character', getObj('graphic', targetID).get("represents"));
//var player = findObjs({type: 'character', id: findObjs({type: 'token', id: targetID})[0].get("represents")})[0].get("controlledby").split(",")[0];
if(tokenChar.get("controlledby").includes(player.get("controlledby")))
{
tokenChar.set("controlledby", "");
}
else
{
if(tokenChar.get("controlledby") != "") { tokenChar.set("controlledby", tokenChar.get("controlledby") + "," + player.get("controlledby")); }
else { tokenChar.set("controlledby", player.get("controlledby")); }
}
}
// receive message to make the thing happen.
on("chat:message", function (msg) {
if ('api'===msg.type && playerIsGM(msg.playerid)){
let player = getObj('player',msg.playerid);
let args = msg.content.split(/\s+/);
let targetID = args[1];
let selectedID = args[2];
if (args[0] === "!TokenControl" && targetID != null && targetID != "" && selectedID != null && selectedID != "") { TokenControl(targetID, msg, selectedID); }
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment