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] AddPlayerCharacter - Simple script for automating adding characters for new players, as well as nice access for existing players.

1558899867
The Aaron
Roll20 Production Team
API Scripter
This is just a little script I threw together for a new game I'm starting.&nbsp; It simplifies adding new characters for new players.&nbsp; I know there are a few others out there, this one just works the way I wanted. =D&nbsp; When a new player joins, it creates them a character and sends them a message: Existing players that join get a list of their existing characters: The Green + adds a new character. Note that nothing shows up for players that are GMs. Script: on('ready',()=&gt;{ const IN_PLAYER_JOURNALS = true; const TIME_TO_CHECK = 1000; const styles = { box: 'display: block; border: 1px solid #999; border-radius: .3em; padding: .3em; background-color: white; box-shadow: 0 0 25px 2px #999; margin: 1em 0 1em 0;', charRow: 'display: block; border-top: 1px solid #ccc; margin: .2em;', img: 'max-width: 2em; max-height: 3em;width:auto;height:auto;border:1px solid #999;float:left;margin: .1em; display: inline-block;', link: 'color: #07c; border: 1px solid #999; border-radius: .3em; padding: .3em 1em; background-color:#ccc;font-weight: bold;display: inline-block;float: right;margin: .1em;', addBtn: 'color: #fff; border: 1px solid #9f9; border-radius: .3em; padding: .3em 1em; background-color:#3c3;font-weight: bold;display: inline-block;float: right;margin: .1em;' }; const defaultImg = '<a href="https://app.roll20.net/images/character.png" rel="nofollow">https://app.roll20.net/images/character.png</a>'; const clear = ()=&gt;`&lt;div style="clear:both"&gt;&lt;/div&gt;`; const GetCharImage = (c) =&gt; c.get('avatar')||defaultImg; const GetShowCharacter = (c) =&gt; `&lt;div style="${styles.charRow}"&gt;&lt;a style="${styles.link}" href="<a href="http://journal.roll20.net/character/${c.id}&quot;&gt;Open&lt;/a&gt;&lt;img" rel="nofollow">http://journal.roll20.net/character/${c.id}"&gt;Open&lt;/a&gt;&lt;img</a> style="${styles.img}" src="${GetCharImage(c)}"/&gt;${c.get('name')}${clear()}&lt;/div&gt;`; const AddButton = () =&gt; `&lt;a style="${styles.addBtn}" href="!add-player-character"&gt;+&lt;/a&gt;`; const AddPlayerCharacter = (player) =&gt; { let c = createObj('character',{ controlledby: player.id, name: `${player.get('displayname')}'s New Character`, inplayerjournals: (IN_PLAYER_JOURNALS ? 'all' : '') }); return c; }; const ShowPlayerCharacters = (player,chars)=&gt;{ chars = chars||findObjs({ type: 'character', archived: false }).filter(c=&gt;c.get('controlledby').split(/\s*,\s*/).includes(player.id)); sendChat('',`/w "${player.get('displayname')}" &lt;div style="${styles.box}"&gt;&lt;div&gt;${AddButton()}Your Characters:${clear()}&lt;/div&gt;${chars.map(GetShowCharacter).join('')}&lt;/div&gt;`); }; const CheckPlayerCharacters = (player) =&gt; { if(playerIsGM(player.id)){ return; } let chars = findObjs({ type: 'character', archived: false }).filter(c=&gt;c.get('controlledby').split(/\s*,\s*/).includes(player.id)); if( ! chars.length){ chars.push(AddPlayerCharacter(player)); } ShowPlayerCharacters(player,chars); }; on('chat:message', (msg) =&gt; { if('api' === msg.type &amp;&amp; /^!add-player-character\b/i.test(msg.content)){ let player = getObj('player',msg.playerid); if(player){ AddPlayerCharacter(player); ShowPlayerCharacters(player); } } }); const checkAll = () =&gt; { findObjs({ type: 'player' }).forEach(CheckPlayerCharacters); }; setTimeout(()=&gt;{ checkAll(); on('change:player:_online',(obj)=&gt;{ if(true === obj.get('online')){ setTimeout(()=&gt;CheckPlayerCharacters(obj),TIME_TO_CHECK); } }); }, TIME_TO_CHECK ); });
This is relevant to my interests! How is it triggered, as I don't see a chat listener for an API command (I didn't look too hard though). Maybe it just shows up each time a players logs in?
1558918467

Edited 1558918483
GiGs
Pro
Sheet Author
API Scripter
It shows up automatically when players log in, thanks to the set Timeout function. See the on(change:player:_online) section. This looks great, Aaron.
1558920303
vÍnce
Pro
Sheet Author
This&nbsp; should be&nbsp;a standard(optional) feature available to all roll20 GM's.&nbsp; That said, it's good to be Pro. ;-P Thanks for another essential script Aaron.
1558920929
GiGs
Pro
Sheet Author
API Scripter
Agreed on it being a standard option. Aaron, I suggest a slightly longer timeout. At 1000, when I Exit Game, the script triggers before shutdown. At 2,000 it doesn't. (I guess this only matters for people who log out using the button, and dont just close the browser window, and also might vary with pc/browser speed.)
1558921834
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Very nice. Added to the Roll20 API Essentials Collection.
1558922736
The Aaron
Roll20 Production Team
API Scripter
Good point on the time. I was thinking about bumping it up a bit anyway so it's after the initial output abs MotD.&nbsp;
1558930144
Loren the GM
Pro
Marketplace Creator
As always, thanks for this, The Aaron. As you say, there are other tools out there, but this definitely seems to work in a way that I want it to work. I'll definitely be making use of this in my new games!