Ir para o conteúdo principal
Version: latest - a1.172.0 ⚖️

📦 Package

Class which represents the current Package

🗿Static ClassClient & Server🧑‍💻API Source

🗿 Static Functions​

Data
FlushPersistentDataFlushes the Persistent Data pending changes to disk immediately
GetPersistentData: tableGets the Persistent Value from the disk
SetPersistentDataSets a Persistent Value which will be saved to disk
Lifecycle
IsUnloading: booleanReturns whether this package is currently unloading
Subscribe: functionSubscribes to an Event
UnsubscribeUnsubscribes from all subscribed Events in this Class and in this Package, optionally passing the function to unsubscribe only that callback
Metadata
GetCompatibilityVersion: stringReturns the package compatibility version
GetName: stringReturns the package folder name
GetTitle: stringReturns the package title
GetVersion: stringReturns the package version
Scripting
ExportMakes any variable available in the global scope
GetDirectories: table of stringGets a list of all directories in this package
GetFiles: table of stringGets a list of all files in this package
LoadFile: functionCompiles a .lua file and returns a function to run it
ReloadClientFileReloads a file from Client/ or Shared/ folder and sends the updated version to clients, triggering the FileReload event on them
Require: anyLoads a .lua file

Datastatic functions​

FlushPersistentDataSlow​

Flushes the Persistent Data pending changes to disk immediately

Note: this operation is already performed automatically under the hood, it is not necessary to keep always flushing and should only be used when really needed, as disk writing is a slow operation

Package.FlushPersistentData()

GetPersistentDataSlow​

Gets the Persistent Value from the disk

Gets a Persistent Value from the disk
Pass no parameter to return the whole table, or a key to get a specific value

local ret = Package.GetPersistentData(key?)
Parameters
NameTypeDescrição
key?stringThe key to get the data Default
Returns
TypeDescrição
tablePersistent values from disk

See also SetPersistentData.

SetPersistentDataModerate​

Sets a Persistent Value which will be saved to disk

Note the actual flush to disk is deferred and may not occur immediately

Package.SetPersistentData(key, value)
Parameters
NameTypeDescrição
keystringKey to index data into. It can be separated by '.' to set a child element
valueanyValue to set at the key

See also GetPersistentData.

Lifecyclestatic functions​

IsUnloadingOptimal​

Returns whether this package is currently unloading

local ret = Package.IsUnloading()
Returns
TypeDescrição
booleanWhether this package is currently unloading

SubscribeOptimal​

Subscribes to an Event

local ret = Package.Subscribe(event_name, callback)
Parameters
NameTypeDescrição
event_namestringEvent to subscribe to
callbackfunctionCallback to run on the event occurring
Returns
TypeDescrição
functionThe function callback

UnsubscribeOptimal​

Unsubscribes from all subscribed Events in this Class and in this Package, optionally passing the function to unsubscribe only that callback

Package.Unsubscribe(event_name, callback?)
Parameters
NameTypeDescrição
event_namestringEvent to unsubscribe to
callback?functionOptional callback to specifically unsubscribe to Default nil

Metadatastatic functions​

GetCompatibilityVersionOptimal​

Returns the package compatibility version

local ret = Package.GetCompatibilityVersion()
Returns
TypeDescrição
stringThe package compatibility version

GetNameOptimal​

Returns the package folder name

local ret = Package.GetName()
Returns
TypeDescrição
stringThe package name/path

GetTitleOptimal​

Returns the package title

local ret = Package.GetTitle()
Returns
TypeDescrição
stringThe package title

GetVersionOptimal​

Returns the package version

local ret = Package.GetVersion()
Returns
TypeDescrição
stringThe package version

Scriptingstatic functions​

ExportOptimal​

Makes any variable available in the global scope

Package.Export(variable_name, value)
Parameters
NameTypeDescrição
variable_namestringName of the variable to export
valueanyValue to be set in the global scope

GetDirectoriesSlow​

Gets a list of all directories in this package

Gets a list of all files in this package, optionally with filters

local ret = Package.GetDirectories(path_filter?)
Parameters
NameTypeDescrição
path_filter?stringPath filter Default
Returns
TypeDescrição
table of stringList of directories

GetFilesSlow​

Gets a list of all files in this package

Filters are optional

local ret = Package.GetFiles(path_filter?, extension_filter?)
Parameters
NameTypeDescrição
path_filter?string or tablePath filter Default
extension_filter?stringExample: .lua Default
Returns
TypeDescrição
table of stringList of files

LoadFileSlow​

Compiles a .lua file and returns a function to run it

To be used for loading files in a sandboxed environment.

Supports the same searchers as Package.Require

local ret = Package.LoadFile(file_path)
Parameters
NameTypeDescrição
file_pathstringPath to the script file to compile
Returns
TypeDescrição
functionthe compiled function to run the script, allowing you to pass an environment table for the script with this format
Examples (1)
Loading a .lua file sandboxed
local func = Package.LoadFile("MyScript.lua")

-- Only allows this file to access Console
local my_env = { Console = Console }

local content = func(my_env)

-- Access all global variables set by the script in my_env
my_env.GlobalVariableFromFile...

See also Require.

ReloadClientFileSlow​

Reloads a file from Client/ or Shared/ folder and sends the updated version to clients, triggering the FileReload event on them

The reloaded file will be re-cached and downloaded normally by new connecting clients

Note that this method does use the main network lane, so it is not recommended to be used frequently or with large files in production environments as it may cause network congestion and lag spikes. It is mostly intended for development purposes to allow faster iteration on client files without needing to reload the whole package

Package.ReloadClientFile(file_path)
Parameters
NameTypeDescrição
file_pathstringThe file path relative to the package root

See also FileReload.

RequireSlow​

Loads a .lua file

Note that this method caches the result, so further calls return the cached value.

We currently support 5 searchers, which are looked in the following order:

  1. Relative to current-file-path/
  2. Relative to current-package/Client/ or current-package/Server/ (depending on your side)
  3. Relative to current-package/Shared/
  4. Relative to current-package/
  5. Relative to Packages/

Note: Clients will only download and have access to Client/ and Shared/ folders.

local ret = Package.Require(file_path, force_load?)
Parameters
NameTypeDescrição
file_pathstringPath to the script file to load
force_load?booleanWhether to force loading this file even if it was already loaded Default false
Returns
TypeDescrição
anyany return values from the included file

See also LoadFile.

🚀 Events​

FileReloadCalled when a file is directly reloaded from the server using Package.ReloadClientFile()
LoadCalled when this package is loaded
UnloadCalled when this package is unloaded

FileReload​

Called when a file is directly reloaded from the server using Package.ReloadClientFile()

You can use the path to reload the file in runtime with Package.Require(path, true)

Package.Subscribe("FileReload", function(file_path)
-- FileReload was called
end)
Arguments
NameTypeDescrição
file_pathstringThe file path

See also ReloadClientFile.

Load​

Called when this package is loaded

This event is triggered differently depending on the situation:

  • When the server starts or you run package reload all the event triggers only after ALL packages are loaded.
  • In all other cases (package load/reload or Package.Load/Reload) the event is triggered immediately after the package is loaded/reloaded.

Package.Subscribe("Load", function()
-- Load was called
end)

Unload​

Called when this package is unloaded

Package.Subscribe("Unload", function()
-- Unload was called
end)