debug.getconstants
Gets all constants from a function.
Syntax
debug.getconstants(func: function | number) -> tableParameters
| Parameter | Type | Description |
|---|---|---|
func | function or number | The function or stack level |
Returns
| Type | Description |
|---|---|
table | An array of all constants in the function |
Description
debug.getconstants returns a table containing all constant values embedded in a function's bytecode. This is useful for inspecting what literal values a function uses.
Example
local function greet(name)
local greeting = "Hello"
local punctuation = "!"
return greeting .. ", " .. name .. punctuation
end
local constants = debug.getconstants(greet)
for i, v in ipairs(constants) do
print(i, type(v), v)
end
--[[
Output:
1 string Hello
2 string !
3 string ,
]]Inspecting Game Functions
-- Find what strings a function uses
local function findStrings(func)
local strings = {}
for i, const in ipairs(debug.getconstants(func)) do
if type(const) == "string" then
table.insert(strings, const)
end
end
return strings
endNotes
- The table is indexed starting at 1
- Some indices may contain
nilfor unused constant slots - Only works with Luau closures
Related Functions
debug.getconstant- Get a single constantdebug.setconstant- Modify a constant