Compiling a string to code

Is there a function built in where someone is able to input a string in a function and that gets executed if it’s valid lua code, or is there some “special module” that you need to import to get this?

There’s loadstring, but it only works on the server with LoadStringEnabled turned on in the ServerScriptService.

Here’s an example

loadstring[[print('hello world')]]() --> prints hello world
loadstring[[return 1 + 1]]() --> reutrns 2

So if you do

loadstring('print("hey")')

It’ll print hey?

And can scripts modify LoadStringEnabled property or no?

If you call that function, yes, as it returns nil if it’s not a valid Lua syntax (or a string binary but that’s probably disabled on Roblox) or a function.

loadstring('print("hey")')()

prints hey.

No, it cannot. (at least accordiing to this)

Why does it need the extra ()?

To call the function created using loadstring.
loadstring returns a nil or a function, loadstring("print('hello')") doesn’t run the code without calling it. Parenthesis are used for function calls.

1 Like