require

Split your scripts into modules

Creating a module

cs2.js

var entityStride = 112;

function getHealth(player)
{
    return entity.getHealth(player);
}

module.exports = 
{
    // non existing funcs
    localPlayer: function () { return entity.getLocalPlayer(); },
    // existing funcs
    health: getHealth,
    stride: entityStride
};

Requiring a module

main.js


✅ When to use a module

Bad for

  • Small scripts without many lines

  • Scripts with only one main feature; such as triggerbot

Best for

  • Large scripts with many functions and extending a lot of functionality

  • Scripts with many features possibly not even related to eachother

  • Creating your own API/Library within the Javascript Engine for self usage or sharing


🧠 Summary

⚠️Security

  • Require scripts are also limited by the "Script access" option in the GUI

  • Internal C# checkage that the require script cannot be loaded from outside of the scripts folder App data

circle-info

Require scripts can use new/existing folders within the scripts folder, so you can create a "require" or "lib" folder

⛓️Scope isolation

  • Variables defined in require file do not leak into global scope

💾Cache

  • Keep function and variable states consistent globally

  • Read and compile require file once

circle-info

Multiple scripts can require the same module (such as entity.js)and will not override each others data

Last updated

Was this helpful?