Bitburner

Bitburner

View Stats:
Herbal Gamer Aug 18, 2023 @ 6:16am
Server purchasing script in Beginners guide returns error
Says at line 20 that the hostname is an empty string. .


'''
/** @param {NS} ns */
export async function main(ns) {
// How much RAM each purchased server will have. In this case, it'll
// be 8GB.
const ram = 8;

// Iterator we'll use for our loop
let i = 0;

// Continuously try to purchase servers until we've reached the maximum
// amount of servers
while (i < ns.getPurchasedServerLimit()) {
// Check if we have enough money to purchase a server
if (ns.getServerMoneyAvailable("home") > ns.getPurchasedServerCost(ram)) {
// If we have enough money, then:
// 1. Purchase the server
// 2. Copy our hacking script onto the newly-purchased server
// 3. Run our hacking script on the newly-purchased server with 3 threads
// 4. Increment our iterator to indicate that we've bought a new server
let hostname = ns.purchaseServer("pserv-" + i, ram);
ns.scp("early-hack-template.js", hostname);
ns.exec("early-hack-template.js", hostname, 3);
++i;
}
//Make the script wait for a second before looping again.
//Removing this line will cause an infinite loop and crash the game.
await ns.sleep(1000);
}
} ''''
Originally posted by X5934 078FR1:
if you are already at maximum purchased servers, the script will fail at ns.scp. The hostname will be an empty string because the purchase failed. So this means this script can be run only once.
Use the scan command in the terminal (from home pc) to see if there are any pserv- servers.
Lets debug if this is your issue.
Replace let i = 0;
with let i = ns.getPurchasedServers ( ).length;
If your script does not fail anymore, then you have definitely reached the maximum.

You can also put in a check to make sure hostname is not an empty string before calling ns.scp and ns.exec, like so:
/** @param {NS} ns */ export async function main ( ns ) { const ram = 8; let i = ns.getPurchasedServers ( ).length; while ( i < ns.getPurchasedServerLimit ( ) ) { if ( ns.getServerMoneyAvailable ( "home" ) > ns.getPurchasedServerCost ( ram ) ) { let hostname = ns.purchaseServer ( "pserv-" + i, ram ); if ( hostname !== "" ) { ns.scp ( "early-hack-template.js", hostname ); ns.exec ( "early-hack-template.js", hostname, 3 ); } ++i; } await ns.sleep ( 1000 ); } }

If you want to try again from the start and don't mind losing the purchase costs, you can delete the servers like so:
/** @param {NS} ns */ export async function main ( ns ) { var pservers; var pserver; pservers = ns.getPurchasedServers ( ); for ( pserver of pservers ) { ns.killall ( pserver ); ns.deleteServer ( pserver ); } }
< >
Showing 1-4 of 4 comments
capitalize the H in "hostname"
for some reason variables that dont start in a capital dont always work.

single letter variables like "i" work fine, but if its more than one letter it seems to need to start in a capital
Last edited by ^4Steel ^1Jackal^2~FYANB~; Aug 18, 2023 @ 10:53am
Herbal Gamer Aug 18, 2023 @ 3:58pm 
Makes no difference. Keeps saying something about empty strings no matter what I try
The author of this thread has indicated that this post answers the original topic.
X5934 078FR1 Aug 18, 2023 @ 10:44pm 
if you are already at maximum purchased servers, the script will fail at ns.scp. The hostname will be an empty string because the purchase failed. So this means this script can be run only once.
Use the scan command in the terminal (from home pc) to see if there are any pserv- servers.
Lets debug if this is your issue.
Replace let i = 0;
with let i = ns.getPurchasedServers ( ).length;
If your script does not fail anymore, then you have definitely reached the maximum.

You can also put in a check to make sure hostname is not an empty string before calling ns.scp and ns.exec, like so:
/** @param {NS} ns */ export async function main ( ns ) { const ram = 8; let i = ns.getPurchasedServers ( ).length; while ( i < ns.getPurchasedServerLimit ( ) ) { if ( ns.getServerMoneyAvailable ( "home" ) > ns.getPurchasedServerCost ( ram ) ) { let hostname = ns.purchaseServer ( "pserv-" + i, ram ); if ( hostname !== "" ) { ns.scp ( "early-hack-template.js", hostname ); ns.exec ( "early-hack-template.js", hostname, 3 ); } ++i; } await ns.sleep ( 1000 ); } }

If you want to try again from the start and don't mind losing the purchase costs, you can delete the servers like so:
/** @param {NS} ns */ export async function main ( ns ) { var pservers; var pserver; pservers = ns.getPurchasedServers ( ); for ( pserver of pservers ) { ns.killall ( pserver ); ns.deleteServer ( pserver ); } }
Last edited by X5934 078FR1; Aug 18, 2023 @ 11:15pm
Herbal Gamer Aug 19, 2023 @ 6:57am 
That seems to have worked, thanks!
< >
Showing 1-4 of 4 comments
Per page: 1530 50