Libraries - tayjay/SCriPt GitHub Wiki

Libraries (require & the lib/ folder)

As your scripts grow you'll want to share helper functions and data between them instead of copy-pasting. SCriPt supports Lua's standard require for exactly this, scoped to a dedicated library folder.

The lib/ folder

Shared library modules live in:

LabAPI/SCriPt/Scripts/lib/

Files in lib/ are not run as scripts on their own (unlike files in global/ or a <port>/ folder). They only load when another script requires them.

Writing a library module

A library module is a normal Lua file that returns a value (usually a table of functions). For example, create Scripts/lib/util.lua:

local util = {}

function util.greet(player)
    player.SendBroadcast("Welcome, " .. player.DisplayName .. "!", 5)
end

function util.count_scps()
    return #Players.ByTeam(Team.SCPs)
end

return util

Using it from a script

In any script in global/ or <port>/, call require with the module name (no .lua, no path):

local util = require("util")   -- loads Scripts/lib/util.lua

greeter = SCriPt.Module('Greeter')

function greeter.load()
    greeter.on(Events.Player.Joined, greeter.onJoined)
end

function greeter.onJoined(args)
    util.greet(args.Player)
end

Name resolution

require("name") looks for, in order:

require(...) Resolves to
require("util") lib/util.lua
require("util") lib/util/init.lua
require("combat.aim") lib/combat/aim.lua (a . becomes a folder separator)

The result is cached — requiring the same module again returns the same value without re-running the file, so modules can safely hold shared state.

Sandbox notes

  • require is available to scripts even while sandboxed.
  • For safety, resolution is locked to the lib/ folder — you cannot require a path outside it (no .. traversal, no absolute paths).
  • The broader loaders (dofile, loadfile, load) are disabled while sandboxed; only require is available. They are re-enabled with the full_access config option (see Dangerous).
⚠️ **GitHub.com Fallback** ⚠️