Scripts
The script functions provide access to script environments and internal state.
getgc
function getgc(includeTables: boolean?): {function | userdata | table}
Returns a list of objects in the Luau garbage collector.
If includeTables
is false, tables will not be included in the list.
Parameters
includeTables
- Whether or not to include tables in the list.
Example
TODO - Write a real-world example use case.
getgenv
function getgenv(): { [string]: any }
Returns the custom global environment of the executor. It can be used to add global functions or share variables between scripts.
Example
Prevent a script from being run twice:
if getgenv().__IS_LOADED then
error("This script is already loaded!")
end
getgenv().__IS_LOADED = true
getloadedmodules
function getloadedmodules(excludeCore: boolean?): {ModuleScript}
Returns a list of ModuleScripts that have been loaded. If excludeCore
is true, CoreScript-related modules will not be included in the list.
Parameters
excludeCore
- Whether or not to exclude core modules from the list.
Example
local modules = getloadedmodules(true)
for _, module in ipairs(modules) do
print(module:GetFullName())
end
getrenv
function getrenv(): { [string]: any }
Returns the global environment of the game client. It can be used to access the global functions that LocalScripts and ModuleScripts use.
Example
Prevent scripts in PlayerScripts from being required:
local refs = {}
local bannedScripts = game:GetService("Players").LocalPlayer.PlayerScripts
refs.require = hookfunction(require, function(...)
local module = ...
if
typeof(module) == "Instance"
and module:IsA("ModuleScript")
and module:IsDescendantOf(bannedScripts)
then
error("You are not allowed to require this module!")
end
return refs.require(...)
end)
getrunningscripts
function getrunningscripts(): {LocalScript | ModuleScript}
Returns a list of scripts that are currently running.
Example
local scripts = getrunningscripts()
for _, object in ipairs(scripts) do
print(object:GetFullName(), "(" .. object.ClassName .. ")")
end
getscriptbytecode
function getscriptbytecode(script: LocalScript | ModuleScript): string
Returns the raw Luau bytecode of the given script.
Parameters
script
- A client-running LocalScript or ModuleScript.
Aliases
dumpstring
Example
local animate = game:GetService("Players").LocalPlayer.Character.Animate
local bytecode = getscriptbytecode(animate)
getscriptclosure
function getscriptclosure(script: LocalScript | ModuleScript): function
Generates a new closure using the bytecode of script
.
Parameters
script
- The script to recreate.
Aliases
getscriptfunction
Example
Compare the return value of a ModuleScript:
local module = game:GetService("CoreGui").RobloxGui.Modules.Common.Constants
local constants = getrenv().require(module)
local generatedConstants = getscriptclosure(module)()
print(constants == generatedConstants) --> false
for k, v in pairs(constants) do
print(k, typeof(v) == typeof(generatedConstants[k])) --> true
end
getscripthash
function getscripthash(script: LocalScript | ModuleScript): string
Returns a SHA384 hash of the script's bytecode. This is useful for detecting changes to a script's source code.
Parameters
script
- A client-running LocalScript or ModuleScript.
Example
local animate = game:GetService("Players").LocalPlayer.Character.Animate
local hash = getscripthash(animate)
task.delay(1.5, function ()
animate.Source = "print('Hello World!')"
end)
for i = 1, 5 do
task.wait(0.5)
local newHash = getscripthash(animate)
if hash ~= newHash then
print("The script has changed!")
hash = newHash
else
print("The script has not changed.")
end
end
getscripts
function getscripts(): {LocalScript | ModuleScript}
Returns a list of every script in the game.
Example
local scripts = getscripts()
for _, object in ipairs(scripts) do
print(object:GetFullName(), "(" .. object.ClassName .. ")")
end
getsenv
function getsenv(script: LocalScript | ModuleScript): { [string]: any }
Returns the global environment of the given script. It can be used to access variables and functions that are not defined as local.
Parameters
script
- A client-running LocalScript or ModuleScript.
Example
local animate = game:GetService("Players").LocalPlayer.Character.Animate
local environment = getsenv(animate)
for k, v in pairs(environment) do
print(k, v, "(" .. typeof(v) .. ")")
end
getthreadidentity
function getthreadidentity(): number
Returns the identity of the current thread.
Learn more about thread identities here.
Aliases
getidentity
getthreadcontext
Example
local identity = getthreadidentity()
print(identity) --> 7
setthreadidentity
function setthreadidentity(identity: number): ()
Sets the current thread identity.
Learn more about thread identities here.
Aliases
setidentity
setthreadcontext
Example
setthreadidentity(3)
print(getthreadidentity()) --> 3
Last updated