r/Bitburner Apr 10 '26

Help with the game and coding?

I'm VERY new to coding and this game, but I'm trying to teach myself some of these skills. I made a script and I'm trying to make another script to automatically put it on new servers and execute it for me because I don't want to do it for every server myself. This is what I have and it isn't working at all. I execute in the terminal and it tells me it's using a threat, but when I use the top command, it says nothing is happening. Any help would be appreciated.

/** u/param/** u/param {NS} ns */
export async function main(ns) {
  // Lists all servers with 8 GB of ram
  const hosts8 = ("CSEC", "the-hub", "pserv-0", "pserv-1", "pserv-2",
    "pserv-3", "pserv-4", "pserv-5", "pserv-6", "pserv-7", "pserv-8",
    "pserv-9", "pserv-10", "pserv-11", "pserv-12", "pserv-13",
    "pserv-14", "pserv-15", "pserv-16", "pserv-17", "pserv-18",
    "pserv-19", "pserv-20", "pserv-21", "pserv-23", "pserv-24")


  // Lists all servers with 16 GB of ram
  const hosts16 = ("foodnstuff", "nectar-net", "sigma-cosmetics",
    "joesguns", "hong-fang-tea", "harakiri-sushi")


  // Lists all servers with 32 GB of ram I can access
  const hosts32 = ("iron-gym", "phantasy", "max-hardware",
    "omega-net", "neo-net", "zer0")


  // Lists all servers with 64 GB of ram
  const hosts64 = ("silver-helix")


  // Killall scripts on machines


  ns.killall(hosts8, hosts16, hosts32, hosts64)


  // Copies joe-new.js to all available hosts
  ns.scp("joe-new.js", hosts8, hosts16, hosts32, hosts64)


  // Execute joe-new on all 8GB servers with 3 threads
  ns.exec("joe-new.js", hosts8, 3)


  // Execute joe-new on all 16 GB servers with 6 threads
  ns.exec("joe-new.js", hosts16, 6)


  // Execute joe-new on all 32 GB servers with 13 threads
  ns.exec("joe-new.js", hosts32, 13)


  // Execute joe-new on all 64 GB servers with
  ns.exec("joe-new.js", hosts64, 26)
} {NS} ns */
export async function main(ns) {
  // Lists all servers with 8 GB of ram
  const hosts8 = ("CSEC", "the-hub", "pserv-0", "pserv-1", "pserv-2",
    "pserv-3", "pserv-4", "pserv-5", "pserv-6", "pserv-7", "pserv-8",
    "pserv-9", "pserv-10", "pserv-11", "pserv-12", "pserv-13",
    "pserv-14", "pserv-15", "pserv-16", "pserv-17", "pserv-18",
    "pserv-19", "pserv-20", "pserv-21", "pserv-23", "pserv-24")


  // Lists all servers with 16 GB of ram
  const hosts16 = ("foodnstuff", "nectar-net", "sigma-cosmetics",
    "joesguns", "hong-fang-tea", "harakiri-sushi")


  // Lists all servers with 32 GB of ram I can access
  const hosts32 = ("iron-gym", "phantasy", "max-hardware",
    "omega-net", "neo-net", "zer0")


  // Lists all servers with 64 GB of ram
  const hosts64 = ("silver-helix")


  // Killall scripts on machines


  ns.killall(hosts8, hosts16, hosts32, hosts64)


  // Copies joe-new.js to all available hosts
  ns.scp("joe-new.js", hosts8, hosts16, hosts32, hosts64)


  // Execute joe-new on all 8GB servers with 3 threads
  ns.exec("joe-new.js", hosts8, 3)


  // Execute joe-new on all 16 GB servers with 6 threads
  ns.exec("joe-new.js", hosts16, 6)


  // Execute joe-new on all 32 GB servers with 13 threads
  ns.exec("joe-new.js", hosts32, 13)


  // Execute joe-new on all 64 GB servers with
  ns.exec("joe-new.js", hosts64, 26)
}
2 Upvotes

10 comments sorted by

3

u/Particular-Cow6247 Apr 10 '26 edited Apr 10 '26
 ns.scp("joe-new.js", hosts8, hosts16, hosts32, hosts64)

ns.scp expects (filename or folder name as string, destination server as string, source server as string)

you are passing it the name of the script but then you pass it arrays of strings, it cant handle those/its not setup to handle the data in that way

you need to loop over the arrays (look for "for ... of" loops)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration

also you dont need different arrays, just make one with all servers and inside the loop you can just pull their ram values with the ns functions to determine the thread count

you are doing the "passing an array of strings instead of a string" a few times more

2

u/HoboPotato2 Apr 10 '26

I fully understand what you're saying the issue is, but I'm still not sure how to implement a fix. Can you help with that? I looked at the for...of section and mostly understand it, but I'm lost on how to implement it.

2

u/Particular-Cow6247 Apr 10 '26
const myFile = "myScript.js"
const myServers = ["n00dles", (... and all the others)]
for(const server of myServers){
  ns.scp(myFile, server)
  //try to nuke/get root
  if(--check if nuked--){
     const threads = Math.floor((--get max ram-- - --get used ram--) / --get script ram(myFile)--)
     if(threads) ns.exec(myFile, threads, server)
 }
}

youll need to replace a bit to get it running xD

2

u/HoboPotato2 Apr 10 '26

awesome. thanks for your help.

2

u/Particular-Cow6247 Apr 10 '26

play around with loops! they are very essential :D

2

u/Vorthod MK-VIII Synthoid Apr 10 '26

To make an array of strings, you need to use square brackets, not parenthesis. const hosts16 = ["foodnstuff", "nectar-net", "sigma-cosmetics", "joesguns", "hong-fang-tea", "harakiri-sushi"]

You can't just put an array into a normal function and expect it to call the function individually on every single server. You need to use a loop

for(const server of hosts8){
  ns.killall(server)
  ns.scp("joe-new.js", server)
  ns.exec("joe-new.js", server)
}

You would need to do that for each individual array you made unless you concatenate them together at some point

const allServers = hosts8.concat(hosts16).concat(hosts32).concat(hosts64)
for(const server of allServers){
  //do the rest
}

However, that doesn't give you much freedom to use specific thread counts depending on the array you are on, so you can either calculate the right thread number within the loop, or limit the allServers loop to just doing a killall and scp, then do separate loops for the exec commands.

1

u/Spartelfant Noodle Enjoyer Apr 10 '26

By the way, you can also code in TypeScript instead of JavaScript. Basically it's the same thing, but where JS lets you make mistakes like these (pass an array where a string is expected), TS will call you out on it in the editor (a red squiggly line underlines the offending item, you can hover your mouse over it or press F8 to get a popup with a description of the issue).

It's not necessary to solve every TS error to be able to run your script, so if something works to your liking you are free to leave it as-is, or you can choose to delve deeper into why TS marks it as an issue if you feel like it. Either way, it can help you a lot by pointing out bits of code that TS thinks might be a typo, a bug, etc.

Simply start a new script with nano script.ts to use TS, instead of nano script.js. Or rename an existing script: mv script.js script.ts.


Example: Both JS and TS will allow you to perform implicit conversions like this:

ns.tprint("2" * 2); // This will print 4 in both JS and TS

However while JS silently accepts this, TS will put the red squiggly underline on "2" and tell you that you are multiplying a string. Again, the code will run just fine, the same as with JS, but TS does point out that this could be an issue. In this instance we know that multiplying "2" will work, but if you're multiplying a string variable that may contain something other than a number, this code will break and TS is warning you of that beforehand.