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

Duplicate Character Sheet + linked Token script

1508944956
PadRpg
Sheet Author
API Scripter
Hi, Before I'm going to start working on that, I would like to know if there's already a script to duplicate a sheet and its linked token ? The TokenMod script saved me some time but not enough :) What do I want? I want to select a token and use a script to duplicate the character sheet associate to it, then copy the token and link the duplicate sheet to it. If it's not already exist, I will work on it :) Thanks,
1508946963

Edited 1508977142
The Aaron
Pro
API Scripter
Nothing exists for doing that, but here ya go! Command is: !dup-char-by-token Select some tokens and run that to create copies of them.  They'll be prefaced with (COPY) for the name so you know which is the new one.  You'll have to drag them out from the panel and if the avatar image of the character is not in a user library (i.e: it's a marketplace image or from the Monster Manual, or the like), it won't set an avatar image. on('ready',()=>{     const simpleObj = (o)=>JSON.parse(JSON.stringify(o));     const getCleanImgsrc = (imgsrc) => {         let parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^?]*)(\?[^?]+)?$/);         if(parts) {             return parts[1]+'thumb'+parts[3]+(parts[4]?parts[4]:`?${Math.round(Math.random()*9999999)}`);         }         return;     };     const duplicateCharacter = (o) => {         let c = simpleObj(o.character);         let oldCid = o.character.id;         delete c.id;         c.name=`(COPY) ${c.name}`;         c.avatar=getCleanImgsrc(c.avatar)||'';         let newC = createObj('character',c);         o.token.set('represents',newC.id);         setDefaultTokenForCharacter(newC,o.token);         o.token.set('represents',oldCid);         _.each(findObjs({type:'attribute',characterid:oldCid}),(a)=>{             let sa = simpleObj(a);             delete sa.id;             delete sa._type;             delete sa._characterid;             sa.characterid = newC.id;             createObj('attribute',sa);         });         _.each(findObjs({type:'ability',characterid:oldCid}),(a)=>{             let sa = simpleObj(a);             delete sa.id;             delete sa._type;             delete sa._characterid;             sa.characterid = newC.id;             createObj('ability',sa);         });     };     on('chat:message',(msg)=>{         if('api'===msg.type && playerIsGM(msg.playerid) && /^!dup-char-by-token\b/.test(msg.content)){             if(msg.selected){                 _.chain(msg.selected)                     .map((o)=>getObj('graphic',o._id))                     .reject(_.isUndefined)                     .map(o=>({token: o, character: getObj('character',o.get('represents'))}))                     .reject(o=>_.isUndefined(o.character))                     .tap(o=>{                         if(!o.length){                             sendChat('',`/w gm <div style="color: #993333;font-weight:bold;">Please select one or more tokens which represent characters.</div>`);                         } else {                             sendChat('',`/w gm <div style="color: #993333;font-weight:bold;">Duplicating: ${o.map((obj)=>obj.character.get('name')).join(', ')}</div>`);                         }                     })                     .each(duplicateCharacter);             } else {                 sendChat('',`/w gm <div style="color: #993333;font-weight:bold;">Please select one or more tokens.</div>`);             }         }     }); });
1508950555
PadRpg
Sheet Author
API Scripter
Thanks, it seems so simple when you answer quickly to this kind of post :) But I've got an error : TypeError: Cannot read property 'substring' of undefined at TrackedObj._validateAttrs (/home/node/d20-api-server/api.js:787:21) at createObj (/home/node/d20-api-server/api.js:1825:23) at duplicateCharacter (apiscript.js:33:20) at Function._.each._.forEach (/home/node/d20-api-server/node_modules/underscore/underscore.js:153:9) at _.(anonymous function) [as each] (/home/node/d20-api-server/node_modules/underscore/underscore.js:1496:34) at on (apiscript.js:67:22) at eval (eval at <anonymous> (/home/node/d20-api-server/api.js:146:1), <anonymous>:65:16) at Object.publish (eval at <anonymous> (/home/node/d20-api-server/api.js:146:1), <anonymous>:70:8) at /home/node/d20-api-server/api.js:1510:12 at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:560
1508952560
The Aaron
Pro
API Scripter
hmm.. anything else of interest in the API console log?
1508952815
PadRpg
Sheet Author
API Scripter
No. As it's in the duplicateCharacter function, I log the c object: {"name":"(COPY) Ogre","bio":"","gmnotes":"","_defaulttoken":"","archived":false,"inplayerjournals":"-KxJE-9Rs2Nx0wZOAvsn","controlledby":"-KxJE-9Rs2Nx0wZOAvsn","_id":"-KxJE3GdhVt2TWKG-8Dj","_type":"character"}
1508953299
The Aaron
Pro
API Scripter
The error is happening for one of the attributes.  Try adding: log(sa);
1508953342
The Aaron
Pro
API Scripter
Basically, one of the attributes on that character is not a valid attribute, so it's failing when it tries to create the copy.
1508956413
PadRpg
Sheet Author
API Scripter
I add logs and it didn't go after "let newC = createObj('character',c);"
1508959410
The Aaron
Pro
API Scripter
Hmm. PM me an invite?
1508977166
The Aaron
Pro
API Scripter
Fixed!  Updated the code above to be correct.  (Already set up in your Game)
1509006416
PadRpg
Sheet Author
API Scripter
Thank you very much. You save me precious time.