> For the complete documentation index, see [llms.txt](https://openjs.wiki.gd/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://openjs.wiki.gd/archive/managing-and-organizing-scripts-with-folders.md).

# Managing and organizing scripts with folders

## Overview:

This guide will explain how to create organized structures with scripts. Since OpenJS 1.3.0, scripts will get automatically loaded even if they are inside a folder and named main.js/Main.js.

### Example:

<figure><img src="https://1928206941-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FninfHRqxK6AQQSdjX6JX%2Fuploads%2FMo80x1E7zB7MnHoPm7hT%2Fimage.png?alt=media&amp;token=79cda8e7-2041-46f5-8aac-100d1994023d" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1928206941-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FninfHRqxK6AQQSdjX6JX%2Fuploads%2FObxnYh8Z9sTOsdtlpejX%2Fimage.png?alt=media&amp;token=aa82c189-bdef-407e-99bb-2c19a89eb6a1" alt=""><figcaption></figcaption></figure>

{% code title="main.js" %}

```js
let module = requireScript("Modules/ExampleModule.js");
// Run functions in main thread because scripts run on an async thread by default
task.main(function() {
    log.info("Total players: " + module.getTotalPlayers());
    log.info("Total entities: " + module.getTotalEntities());
    log.info("Total worlds: " + module.getWorldCount());
})
```

{% endcode %}

{% code title="Modules/ExampleModule.js" %}

```js
var Bukkit = org.bukkit.Bukkit

return { 
    getTotalPlayers: function() {
        try {
            return Bukkit.getOnlinePlayers().size();
        } catch (error) {
            log.error("Error getting player count: " + error);
            return 0;
        }
    },

    getTotalEntities: function() {
        var totalEntities = 0;

        try {
            var worlds = Bukkit.getWorlds();

            for (var i = 0; i < worlds.size(); i++) {
                var world = worlds.get(i);
                var entities = world.getEntities();
                totalEntities += entities.size();
            }

            return totalEntities;
        } catch (error) {
            log.error("Error getting entity count: " + error);
            return 0;
        }
    },

    getWorldCount: function() {
        return Bukkit.getWorlds().size();
    }
}
```

{% endcode %}

### **Output:**

<figure><img src="https://1928206941-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FninfHRqxK6AQQSdjX6JX%2Fuploads%2FrXCtC9q5Cz0mtNox1CwW%2Fimage.png?alt=media&amp;token=b5c02478-455d-463c-9edd-be63ddc41b24" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}

* The **name of scripts that are inside folders** are **their folders name** (as seen in the output)
* Required scripts will run under the same environment as the main script, more info [here](/apis-and-tools/globals/requirescript.md)
* Use the [**script-class**](/apis-and-tools/globals/script.md) to get the information on the current script (name, path, etc)
* If you want to **jump out of the path** via any function that expects a path use ".." Example:\
  Current path: `"C:\Folder\CurrentFolder\main.js"`\
  Target path: `"C\:Folder\someScript.js"`\
  Final path: `"..\someScript.js"` ⇒ `"C\:Folder\someScript.js"`
  {% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://openjs.wiki.gd/archive/managing-and-organizing-scripts-with-folders.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
