0

I'm trying to pause this function using the breakTheLoop var but it wont work. This is the code if anyone could help me out `

    async afk(message) {
        var breakTheLoop = false;
        while (!breakTheLoop) {

        await new Promise(resolve => setTimeout(resolve, 500));
        this.bot.setControlState('forward', true);
        await this.bot.waitForTicks(1);
        //this.bot.setControlState('sprint', true);
        this.bot.setControlState('jump', true);
        let arm = Math.random() < 0.5 ? 'right' : 'left';
        await this.bot.swingArm(arm);
        let yaw = 2*Math.random()*Math.PI - (0.5*Math.PI);
        let pitch = Math.random()*Math.PI - (0.5*Math.PI);
        await this.bot.look(yaw,pitch,false);
        await this.bot.waitForTicks(11);
        if (message === '!afkoff') {
            return breakTheLoop = true;
        }
        
    }}

This is the function i use to call to the async function

chatLog(username, message) {
    if (!botNames.includes(username)) {
        this.log(chalk.ansi256(98)(`<${username}>`), message)
    if(message === '!afk')this.afk(), this.bot.chat("I'm AFK!");
    }
}

I'm new to js so any help would be appreciated.

Expected the loop to stop but it just keeps going i've tried multiple answers on here.

  • you are not passing anything for the `message` parameter on the `afk` function. Even if you were you are only calling the `afk` function if the message is `"!afk"`. So it will never be `"!afkoff"` and your `message === "!afkoff"` condition will never return true. – about14sheep Nov 27 '22 at 18:27
  • Sry for the question i don't have much experience in js but could you explain how i could pass the message parameter in the afk func? Or just how to do it correctly? I've been trying for a while but nothing that I try seems to work. Thanks for the response! – Lil Poop Nov 27 '22 at 19:07
  • you would pass it in the parentheses so `this.afk(message)`. However, as i said earlier, as written you are only calling `this.afk()` if the message is `"!afk"` which means your condition `message === '!afkoff'` will never be true so even if you do pass it, it wont stop the looping – about14sheep Nov 27 '22 at 19:15
  • Ok I understand so any way to actually stop the loop when i pass the `!afkoff` message? Thanks for responding, it's really helpful. – Lil Poop Nov 27 '22 at 19:18
  • if you call `this.afk` with `'!afk'` it will continue to run. Even if you then call `this.afk` with `'!afkoff'` it will not stop the first instance looping, it will only run everything in the function and then return at the end (as if the while loop was not there). As written, unless you only call the `this.afk` function with a message value of `'!afkoff'` it will always continuously loop. – about14sheep Nov 27 '22 at 19:26
  • I think you should take a step back, and understand [scope in javascript](https://developer.mozilla.org/en-US/docs/Glossary/Scope) and then attempt this again. If you have any issues there, come back and ask a new question and we can help debug – about14sheep Nov 27 '22 at 19:29

0 Answers0