Ark
Ark
Error message on Roll: await is only valid in async functions and the top level bodies of modules
I am trying to get the .total value on a roll but I am not able to. I know Roll is async and you have to await for the roll to be finalised before I can get the total but I get this error message on the console when I try to use await: await is only valid in async functions and the top level bodies of modules This is my code: function on the main class of my module:
static getButton(userID){

let _button = document.createElement("button");
_button.className = "turn-icon-button flex0";
_button.title = "test";

//d20 icon
let _icon = document.createElement("i");
_icon.className = "fa-solid fa-dice-d20";

_button.append(_icon);

_button.addEventListener("click",function(event){
let r = new Roll("1d20");
//await r.toMessage();
//await r.evaluate();
r.toMessage();
console.log(r.total)
});

return _button;

}
static getButton(userID){

let _button = document.createElement("button");
_button.className = "turn-icon-button flex0";
_button.title = "test";

//d20 icon
let _icon = document.createElement("i");
_icon.className = "fa-solid fa-dice-d20";

_button.append(_icon);

_button.addEventListener("click",function(event){
let r = new Roll("1d20");
//await r.toMessage();
//await r.evaluate();
r.toMessage();
console.log(r.total)
});

return _button;

}
Hook outside my class where the getButton function is used:
Hooks.on('renderPlayerList', (playerList, html) => {
if (game.user.isGM){
const loggedInUserList = html.find(".player");
for (var i = loggedInUserList.length - 1; i >= 0; i--) {
loggedInUserList[i].append(mymodule.getButton(loggedInUserList[i].getAttribute("data-user-id")))
}
}
Hooks.on('renderPlayerList', (playerList, html) => {
if (game.user.isGM){
const loggedInUserList = html.find(".player");
for (var i = loggedInUserList.length - 1; i >= 0; i--) {
loggedInUserList[i].append(mymodule.getButton(loggedInUserList[i].getAttribute("data-user-id")))
}
}
Any help would be appreciated. Cheers
5 replies