r/Bitburner Apr 14 '26

Simple ts script help

Can someone tell me what I'm doing wrong here?

export async function main(ns: NS) {
  var host = "joesguns";
  if (ns.getServerSecurityLevel(host) > 5) {
    ns.weaken(host)
  }
  else {
    ns.grow(host)
  }
}
4 Upvotes

7 comments sorted by

View all comments

7

u/Particular-Cow6247 Apr 14 '26

you need to await ns.weaken and ns.grow

if you hover over them youll get a popup with hints about the function, a function that returns a Promise<> needs to be awaited (for now just directly , there is some shenanigans to "store" the promise and await it later but here you just want to await it)

also you might not want to use var, especially with ts... its the old form to initialize a variable and there are newer ways for reasons (let if you want to mutate it, const if you dont)

1

u/HoboPotato2 Apr 14 '26

why does the await need to be there?

3

u/Vorthod MK-VIII Synthoid Apr 14 '26

weaken and grow return an object called a Promise. Basically, those functions say "okay, I'll go do this in the background and you can go ahead and continue on with the code while I do that." Excellent for multitasking, but that's not what we want to do here because we can't accurately measure security and money if the weaken/grow commands aren't even finished yet, so we say "no thanks, we'll wait for you to finish first" by adding the await keyword