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.
loadstring(source: string, chunk_name: string?) -> function?, string?| Parameter | Type | Default | Description |
|---|---|---|---|
source | string | — | The Lua source code to compile |
chunk_name | string? | nil | Optional name for the chunk (used in error messages) |
Returns: The compiled function, or nil + error message on failure.
Example
local func, err = loadstring('print("Hello from loadstring!")')
if func then
func()
else
warn("Compile error:", err)
endloadfile
Loads and compiles a file from the workspace folder. Equivalent to loadstring(readfile(path)).
loadfile(path: string) -> function?, string?| Parameter | Type | Description |
|---|---|---|
path | string | Relative path to the file in the workspace |
Example
local func = loadfile("scripts/init.lua")
if func then
func()
enddofile
Executes a file from the workspace folder and returns its results. Equivalent to loadfile(path)().
dofile(path: string) -> any...| Parameter | Type | Description |
|---|---|---|
path | string | Relative path to the file in the workspace |
Example
-- 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.
decompile(script: LocalScript | ModuleScript) -> string| Parameter | Type | Description |
|---|---|---|
script | LocalScript | ModuleScript | The script to decompile |
Example
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
queueonteleport(script: string) -> void| Parameter | Type | Description |
|---|---|---|
script | string | The Lua source code to execute after teleport |
Example
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
clearteleportscript() -> voidExample
queueonteleport('print("test")')
clearteleportscript() -- Removes the queued scriptScript Information
getcallingscript
Returns the script Instance that called the current function.
getcallingscript() -> BaseScript?Example
local caller = getcallingscript()
if caller then
print("Called from:", caller:GetFullName())
endgetscriptfromthread
Returns the script Instance associated with a given thread.
getscriptfromthread(thread: thread) -> BaseScript?| Parameter | Type | Description |
|---|---|---|
thread | thread | The thread to get the script from |
Example
local threads = getallthreads()
for _, t in pairs(threads) do
local script = getscriptfromthread(t)
if script then
print(script:GetFullName())
end
endgetthreadfromfunction
Returns the thread associated with a given function.
Aliases: getfunctionthread
getthreadfromfunction(func: function) -> thread?| Parameter | Type | Description |
|---|---|---|
func | function | The function to get the thread from |
Example
local func = getreg()[5] -- some function from registry
local thread = getthreadfromfunction(func)
if thread then
print("Found thread for function")
endgetscriptfromfunction
Returns the script Instance associated with a given function.
Aliases: getfunctionscript
getscriptfromfunction(func: function) -> BaseScript?| Parameter | Type | Description |
|---|---|---|
func | function | The function to get the script from |
Example
local func = getreg()[10] -- some function from registry
local script = getscriptfromfunction(func)
if script then
print("Function belongs to:", script:GetFullName())
endgetfunctionreference
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
getfunctionreference(func: function, return_thread: boolean?) -> BaseScript | thread| Parameter | Type | Default | Description |
|---|---|---|---|
func | function | — | The function to get the reference of |
return_thread | boolean? | false | If true, returns the thread instead of the script |
Returns:
- When
return_threadisfalse(default): theBaseScriptthat uses the function - When
return_threadistrue: thethreadassociated with the function
Example
-- 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
getscriptclosure(script: LocalScript | ModuleScript) -> function| Parameter | Type | Description |
|---|---|---|
script | LocalScript | ModuleScript | The script to get the closure from |
Example
local closure = getscriptclosure(game.Players.LocalPlayer.PlayerScripts.PlayerModule)
print(closure) --> functiongetscriptbytecode
Returns the raw bytecode of a script.
Aliases: getscriptsource, dumpstring
getscriptbytecode(script: LocalScript | ModuleScript) -> string| Parameter | Type | Description |
|---|---|---|
script | LocalScript | ModuleScript | The script to get bytecode from |
Example
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.
getscripthash(script: LocalScript | ModuleScript) -> string| Parameter | Type | Description |
|---|---|---|
script | LocalScript | ModuleScript | The script to hash |
Example
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
getsenv(script: LuaSourceContainer) -> table| Parameter | Type | Description |
|---|---|---|
script | LuaSourceContainer | The script to get the environment of |
Example
-- 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
endgetfunctionhash
Returns a hash string representing a function's bytecode.
getfunctionhash(func: function) -> string| Parameter | Type | Description |
|---|---|---|
func | function | The function to hash |
Example
local hash = getfunctionhash(print)
print("Function hash:", hash)comparefunctionhash
Compares a function's hash against a given hash string. Returns true if they match.
comparefunctionhash(func: function, hash: string) -> boolean| Parameter | Type | Description |
|---|---|---|
func | function | The function to compare |
hash | string | The expected hash |
Example
local hash = getfunctionhash(print)
print(comparefunctionhash(print, hash)) --> true