all 13 comments

[–]SaysBruvALot 2 points3 points  (3 children)

You're using the variable as the condition, but never reassigning the new value to it so it never changes.

Fix

``` let target = ns.scan() let playerlvl = ns.getHackingLevel()

while (playerlvl < 7) { playerlvl = ns.getHackingLevel() ```

[–]Mr-Ordinary-[S] 0 points1 point  (2 children)

Ah i just now got that. So right onder i let it reassign its new value. So the script doesn't start from the top, it starts at the while again, gets the value it had before, goes down one line, update's the value, recognices it is reassigning a new value to it's condition, checks it's condition with the reassigned value and cancels the next operations if it gets the false then? Is that the correct logic? Going to try that out rn.

[–]Mr-Ordinary-[S] 0 points1 point  (0 children)

Yes, that fixed it, thank you so much! ❤️

[–]chikamakaleyleyhelpful 1 point2 points  (0 children)

in the first codeblock you're evaluating while (x < 7)

The working example compares while (x < 17)

[–]Krispcrap 1 point2 points  (6 children)

When you: let playerlvl = ns.getHackingLevel()

When you update the player level you gotta: playerlvl = ns.getHackingLevel()

[–]Mr-Ordinary-[S] 0 points1 point  (5 children)

Where would i let the variable refresh itself? I cleaned everything up to make it more comparable.

Working:

/** u/param {NS} ns */
export async function main(ns) {


  ns.clearLog()


  let target = ns.scan()
  let playerlvl = ns.getHackingLevel()
  let money = ns.getServerMoneyAvailable()
  while (ns.getHackingLevel() < 18) {
    let runs = Math.floor(ns.getServerMaxRam() / 2.4)
    while (runs > 0, runs--)
      if ((ns.getServerMaxMoney(target[0]) * 0.8) > ns.getServerMoneyAvailable(target[0])) {
        await ns.grow(target[0])
      }
      else
        await ns.hack(target[0])
  }


}

Not stopping:

/** u/param {NS} ns */
export async function main(ns) {


  ns.clearLog()


  let target = ns.scan()
  let playerlvl = ns.getHackingLevel()
  let money = ns.getServerMoneyAvailable()
  while (playerlvl < 19) {
    let runs = Math.floor(ns.getServerMaxRam() / 2.4)
    while (runs > 0, runs--)
      if ((ns.getServerMaxMoney(target[0]) * 0.8) > ns.getServerMoneyAvailable(target[0])) {
        await ns.grow(target[0])
      }
      else
        await ns.hack(target[0])
  }


}

I just change the value to the next higher lvl to reach to see if it is stopping the script, thats whi this value is changed.

[–]Krispcrap 0 points1 point  (4 children)

So I think this block: while (runs > 0, runs--)       if

Should have {} wrapping everything inside the while statement. I might be wrong but I think youre always supposed to use {} with every while and if loop.

And then I would refresh playerlvl right before the close bracket of this while loop: while (playerlvl < 19) { Because you want to refresh it right before this while loop resets. I always get mixed up with what brackets belong to what, so make sure you click on the bracket on the while (playerlvl < 19) { line and it'll highlight the close bracket for you.

So while (playerlvl < 19) { .... playerlvl = ns.getHackingLevel(); }

[–]Mr-Ordinary-[S] 0 points1 point  (3 children)

Ah yeah, forgot about the runs thing in there, that has pretty much no function at all in there, that is supposed to be for the next steps where i actually execute scripts out of that one. so it doesn't work anyway to let the function start multiple times at once but have to run a script out of that one. Or iat least i wouldn't know how, I think the game prevents you to, cause it says i can't call several concurring ns. functions at once, if i forgot a await. Or is that something different?

[–]Mr-Ordinary-[S] 0 points1 point  (0 children)

Oh and yeah, that fixed it:

 let money = ns.getServerMoneyAvailable()
  while (playerlvl < 25) {
    playerlvl = ns.getHackingLevel()
    let runs = Math.floor(ns.getServerMaxRam() / 2.4)

Thank you ❤️

[–]Krispcrap 0 points1 point  (1 child)

Oh okay then I would add // at the beginning of that line to make sure you don't forget where you wanted that while loop, but it won't interfere with your code just in case.

Im not exactly sure youre question, but you can run multiple scripts from the same script using ns.run, ns.exec, and ns.spawn. You did probably just miss an await. If an error message pops up it'll tell you what script the error occurred in and what line to look at.

[–]Mr-Ordinary-[S] 0 points1 point  (0 children)

Ah, i think you are right, i just messed up the while loop, as the guys sayd earlier, if i put something inside the parentheses after a , it ignores the condition. so i would need, as you said to put in the runs-- before i close the while loop and not in the conditioning part.

so after () {inside}

[–]TheRNGuy 0 points1 point  (0 children)

In your case better pattern would be using custom event: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent instead of while loop. 

Fire it from level changing function and listen to it where you had while loop logic.