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

frustrated newbie

1412201941

Edited 1412201982
All of the tutorial videos i found just show people copying 1100 lines of code into a window and hitting save, useless. why does this do absolutely nothing when i type !rt in chat? on("chat:message", function(msg) { log("I an alive"); if(msg.type == "api" && msg.content.indexOf("!rt ") !== -1) { log ("I am super alive"); } });
Looks like you have a space after the !rt in the string indexOf. If you remove that !rt should trigger and it should also trigger on something like "!rt test". Roll20 probably trims the content input for leading/trailing spaces.
Im gonna go dig a hole and bury myself. It was indeed the blank space, although i am annoyed that log doesnt print to the roll20 chat either. just to the sandbox log on the api entry page. but since you are not on there when the chat event triggers, its useless. But thank you! As Homer said, urge to kill fading...
Well the log is mostly for logging for information/debugging purposes. You can certainly have it open in another tab and the Roll20 app in another one and test that way.
1412205427
esampson
Pro
Sheet Author
The purpose of log is to provide an output that doesn't occur in the chat window. If you want the data sent to the chat window you can easily do so by using sendChat instead.
is randominteger broken as well? i am having to use math.random and floor, the example from the docs is getting undefined
1412207477
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
on("ready", function() { on("chat:message", function (msg) { if (msg.type != "api") return; msg.who = msg.who.replace(" (GM)", ""); msg.content = msg.content.replace("(GM) ", ""); var command = msg.content.split(" ", 1); if(command == "!thoughtoftheday") { deepthought(); }; }); }); deepthought = function() { var randomNumber = Math.floor(Math.random() * 100) + 1 if (randomNumber < 50){ sendChat("My API thinks", "All work and no play makes Jack a dull boy.") }else{ sendChat("My API thinks", "The sun will come out tomorrow.") } };
1412221563
Lithl
Pro
Sheet Author
API Scripter
Molgrin [Matthew P.] said: is randominteger broken as well? i am having to use math.random and floor, the example from the docs is getting undefined No, randominteger is not broken. Please share the code you're trying to use it with and maybe we can find the problem.
Hi Molgrin. There are a couple of tools that will help you write, format and debug your js. I use Editey as an add-on for my Google Drive and it's been really good for me. No editor is going to catch the issue above with the extra space, but the tools do help eliminate other issues. Another you can try is <a href="http://www.jshint.com/" rel="nofollow">http://www.jshint.com/</a> which allows you to copy and past code into the page. Errors or flags are output on the right. Just ignore any errors about Roll20-specific variables not being defined.
1412774831

Edited 1412869560
ReferenceError: randominteger is not defined at Sandbox.&lt;anonymous&gt; (evalmachine.&lt;anonymous&gt;:11:18) at eval ( if i use the math functions it works fine from on("chat:message", function(msg) { if(msg.type == "api" && msg.content.indexOf("!rt") !== -1) { var targets = ['Player A', 'Player B', 'Catfish', 'Sally', 'Drake','Fred','Barney', 'Daffy Duck']; var min = 1; var max = targets.length; var attacker =msg.content.slice("!rt ".length); // var target = Math.floor(Math.random() * (max)) + min; var target = randominteger(max); sendChat(attacker, " attacks " + targets[target-1]); } });
Stephen thank you for that excellent skeleton!
1412775477

Edited 1412775636
The Aaron
Roll20 Production Team
API Scripter
It's case sensitive, so this is an error: var target = randominteger(max); This is not: var target = randomInteger(max); (Also, it's easier to read the code if you put it in a code block. You can do that by selecting it and hitting the Paragraph Symbol [ ¶ ] and selecting code from the drop-down.)
ty for code block tip, i didnt see it at first glance. I guess i spent too much time coding Perl hehe. It escaped me totally it might be case sensitive, thx all.
1412783968
The Aaron
Roll20 Production Team
API Scripter
No worries. It's hard to move between case-insensitive and case-sensitive languages. =D Another tip for you, you can go back and edit prior posts and add that code formatting in. =D
thanks aaron!