getscriptclosure

Gets the main function of a script.

Syntax

getscriptclosure(script: LocalScript | ModuleScript | Script) -> function | string

Parameters

ParameterTypeDescription
scriptLocalScript, ModuleScript, or client ScriptThe script

Returns

TypeDescription
functionA newly compiled closure on success
stringA compilation error message

Description

getscriptclosure compiles a new closure from a script's bytecode. It is not the closure currently running in the game, and for a ModuleScript it is not the value returned by require().

Example

local scripts = getrunningscripts()
if #scripts > 0 then
    local closure = getscriptclosure(scripts[1])
    print("Got closure:", closure)
    local constants = debug.getconstants(closure)
    print("Constants:", #constants)
end

Accessing Module Functions

local modules = getloadedmodules()
for _, module in ipairs(modules) do
    local closure = getscriptclosure(module)

    local protos = debug.getprotos(closure)
    print(module.Name, "has", #protos, "inner functions")
end

If the bytecode cannot be read, the function raises an error. A string result indicates that compilation failed.

Aliases

  • getscriptfunction