Skip to content

loadstring

Unsafe by design

Compiles the given string, and returns it runnable in a function. The environment must become unsafe after this function is called due to it allowing the modification of globals uncontrollably (see setfenv/getfenv documentation).

Notes on loadstring

If the source compiles sucecssfully, you will get a runnable function that, when called, will execute the source you provided along with the passed arguments. If the source fails to compile, you will instead receive nil alongside the error, explaining what went wrong.

function loadstring<A...>(source: string, chunkname: string?): (((A...) -> any) | nil, string?)

Parameters

Parameter Description
source The source code string to compile.
chunkname? Custom chunk name.

Examples

Example 1

Compiling and running source code successfully
1
2
3
4
5
loadstring([[
    placeholder = {"Example"}
]])()

print(placeholder[1]) -- Output: Example

Example 2

Using a custom chunk name while also getting an error
1
2
3
4
local func, err = loadstring("Example = ", "CustomChunk")

print(func) -- Output: nil
print(err)  -- Output: [string "CustomChunk"]:1: Expected identifier when parsing expression, got <eof>