DOCUMENTATION: Update tutorial script for buying cloud servers (#2653)

This commit is contained in:
catloversg
2026-04-13 07:05:56 +07:00
committed by GitHub
parent cc9144c01b
commit 9ab3e0bcb4
2 changed files with 10 additions and 11 deletions

View File

@@ -367,12 +367,11 @@ Paste the following code into the [Script](../basic/scripts.md) editor:
/** @param {NS} ns */
export async function main(ns) {
// How much RAM each cloud server will have. In this case, it'll
// be 8GB.
// How much RAM each cloud server will have. In this case, it'll be 8GB.
const ram = 8;
// Iterator we'll use for our loop
let i = 0;
let i = ns.cloud.getServerNames().length;
// Continuously try to purchase cloud servers until we've reached the maximum
// amount of servers
@@ -381,16 +380,16 @@ Paste the following code into the [Script](../basic/scripts.md) editor:
if (ns.getServerMoneyAvailable("home") > ns.cloud.getRamLimit(ram)) {
// If we have enough money, then:
// 1. Purchase the server
// 2. Copy our hacking script onto the newly-purchased cloud server
// 3. Run our hacking script on the newly-purchased cloud server with 3 threads
// 2. Copy our hacking script onto the newly purchased cloud server
// 3. Run our hacking script on the newly purchased cloud server with 3 threads
// 4. Increment our iterator to indicate that we've bought a new server
let hostname = ns.cloud.purchaseServer("cloud-server-" + i, ram);
const hostname = ns.cloud.purchaseServer("cloud-server-" + 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.
// 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);
}
}

View File

@@ -33,12 +33,12 @@ Adding a sleep like in the first example, or changing the code so that the `awai
Common infinite loop when translating the server purchasing script in starting guide to scripts is to have a while loop, where the condition's change is conditional:
var ram = 8;
var i = 0;
const ram = 8;
let i = ns.cloud.getServerNames().length;
while (i < ns.cloud.getServerLimit()) {
if (ns.getServerMoneyAvailable("home") > ns.cloud.getRamLimit(ram)) {
var hostname = ns.cloud.purchaseServer("cloud-server-" + i, ram);
const hostname = ns.cloud.purchaseServer("cloud-server-" + i, ram);
ns.scp("early-hack-template.js", hostname);
ns.exec("early-hack-template.js", hostname, 3);
++i;