Skip to content

Script

Functions for loading, executing, analyzing, and inspecting scripts.

Script Execution

loadstring

Compiles a Lua source string into a function. Equivalent to Lua 5.1's loadstring.

lua
loadstring(source: string, chunk_name: string?) -> function?, string?
ParameterTypeDefaultDescription
sourcestringThe Lua source code to compile
chunk_namestring?nilOptional name for the chunk (used in error messages)

Returns: The compiled function, or nil + error message on failure.

Example

lua
local func, err = loadstring('print("Hello from loadstring!")')
if func then
    func()
else
    warn("Compile error:", err)
end

loadfile

Loads and compiles a file from the workspace folder. Equivalent to loadstring(readfile(path)).

lua
loadfile(path: string) -> function?, string?
ParameterTypeDescription
pathstringRelative path to the file in the workspace

Example

lua
local func = loadfile("scripts/init.lua")
if func then
    func()
end

dofile

Executes a file from the workspace folder and returns its results. Equivalent to loadfile(path)().

lua
dofile(path: string) -> any...
ParameterTypeDescription
pathstringRelative path to the file in the workspace

Example

lua
-- Execute a config file and get its return value
local config = dofile("config.lua")
print(config.setting)

decompile

Decompiles a script back into readable Lua source code.

lua
decompile(script: LocalScript | ModuleScript) -> string
ParameterTypeDescription
scriptLocalScript | ModuleScriptThe script to decompile

Example

lua
local source = decompile(game.Players.LocalPlayer.PlayerScripts.PlayerModule)
print(source)

Teleport Queue

queueonteleport

Queues a Lua script to execute after the player teleports to a new server/place.

Aliases: queue_on_teleport, addteleportscript, queueteleportscript, addqueuescript

lua
queueonteleport(script: string) -> void
ParameterTypeDescription
scriptstringThe Lua source code to execute after teleport

Example

lua
queueonteleport([[
    print("Loaded after teleport!")
    loadstring(game:HttpGet("https://example.com/script.lua"))()
]])

clearteleportscript

Clears all scripts that were queued for execution after teleport.

Aliases: clearqueuescript

lua
clearteleportscript() -> void

Example

lua
queueonteleport('print("test")')
clearteleportscript() -- Removes the queued script

Script Information

getcallingscript

Returns the script Instance that called the current function.

lua
getcallingscript() -> BaseScript?

Example

lua
local caller = getcallingscript()
if caller then
    print("Called from:", caller:GetFullName())
end

getscriptfromthread

Returns the script Instance associated with a given thread.

lua
getscriptfromthread(thread: thread) -> BaseScript?
ParameterTypeDescription
threadthreadThe thread to get the script from

Example

lua
local threads = getallthreads()
for _, t in pairs(threads) do
    local script = getscriptfromthread(t)
    if script then
        print(script:GetFullName())
    end
end

getthreadfromfunction

Returns the thread associated with a given function.

Aliases: getfunctionthread

lua
getthreadfromfunction(func: function) -> thread?
ParameterTypeDescription
funcfunctionThe function to get the thread from

Example

lua
local func = getreg()[5] -- some function from registry
local thread = getthreadfromfunction(func)
if thread then
    print("Found thread for function")
end

getscriptfromfunction

Returns the script Instance associated with a given function.

Aliases: getfunctionscript

lua
getscriptfromfunction(func: function) -> BaseScript?
ParameterTypeDescription
funcfunctionThe function to get the script from

Example

lua
local func = getreg()[10] -- some function from registry
local script = getscriptfromfunction(func)
if script then
    print("Function belongs to:", script:GetFullName())
end

getfunctionreference

Returns the script or thread associated with a function. By default returns the BaseScript that uses the function. If return_thread is true, returns the thread instead.

Aliases: getfunctionref

lua
getfunctionreference(func: function, return_thread: boolean?) -> BaseScript | thread
ParameterTypeDefaultDescription
funcfunctionThe function to get the reference of
return_threadboolean?falseIf true, returns the thread instead of the script

Returns:

  • When return_thread is false (default): the BaseScript that uses the function
  • When return_thread is true: the thread associated with the function

Example

lua
-- Get the script that owns a function
local script = getfunctionreference(some_function)
print(script:GetFullName())

-- Get the thread instead
local thread = getfunctionreference(some_function, true)
print(thread)

getscriptclosure

Returns the top-level closure (function) of a script.

Aliases: getscriptfunction

lua
getscriptclosure(script: LocalScript | ModuleScript) -> function
ParameterTypeDescription
scriptLocalScript | ModuleScriptThe script to get the closure from

Example

lua
local closure = getscriptclosure(game.Players.LocalPlayer.PlayerScripts.PlayerModule)
print(closure) --> function

getscriptbytecode

Returns the raw bytecode of a script.

Aliases: getscriptsource, dumpstring

lua
getscriptbytecode(script: LocalScript | ModuleScript) -> string
ParameterTypeDescription
scriptLocalScript | ModuleScriptThe script to get bytecode from

Example

lua
local bytecode = getscriptbytecode(game.Players.LocalPlayer.PlayerScripts.PlayerModule)
print(#bytecode, "bytes")

getscripthash

Returns a hash string representing the script's contents. Useful for detecting script modifications.

lua
getscripthash(script: LocalScript | ModuleScript) -> string
ParameterTypeDescription
scriptLocalScript | ModuleScriptThe script to hash

Example

lua
local hash = getscripthash(game.Players.LocalPlayer.PlayerScripts.PlayerModule)
print("Script hash:", hash)

Script Environment

getsenv

Returns the environment table of a running LuaSourceContainer (LocalScript, ModuleScript, etc.).

Aliases: getscriptenvironment, getscriptenv, getscriptglobal, getmenv

lua
getsenv(script: LuaSourceContainer) -> table
ParameterTypeDescription
scriptLuaSourceContainerThe script to get the environment of

Example

lua
-- Get a LocalScript's environment and read its variables
local playerModule = game.Players.LocalPlayer.PlayerScripts:FindFirstChild("PlayerModule")
if playerModule then
    local env = getsenv(playerModule)
    for k, v in pairs(env) do
        print(k, type(v))
    end
end

getfunctionhash

Returns a hash string representing a function's bytecode.

lua
getfunctionhash(func: function) -> string
ParameterTypeDescription
funcfunctionThe function to hash

Example

lua
local hash = getfunctionhash(print)
print("Function hash:", hash)

comparefunctionhash

Compares a function's hash against a given hash string. Returns true if they match.

lua
comparefunctionhash(func: function, hash: string) -> boolean
ParameterTypeDescription
funcfunctionThe function to compare
hashstringThe expected hash

Example

lua
local hash = getfunctionhash(print)
print(comparefunctionhash(print, hash)) --> true