Garry's Mod Wiki


function, string CompileString( string code, string identifier, boolean HandleError = true )


This function will compile the code argument as lua code and return a function that will execute that code.

Please note that this function will not automatically execute the given code after compiling it.


1 string code
The code to compile.
2 string identifier
An identifier in case an error is thrown. (The same identifier can be used multiple times)
3 boolean HandleError = true
If false this function will return an error string instead of throwing an error.


1 function
A function that, when called, will execute the given code. Returns nil if there was an error.
2 string
The error string. Will be nil if there were no errors or the function handles errors (third argument is true).


Code that will not compile, with ErrorHandling set to false.

local code = "MsgN('Hi)" local func = CompileString(code, "TestCode", false) MsgN(func)
Output: TestCode:1: unfinished string near '<eof>' (this is not a script error - it is a returned string)


Code that will compile.

local code = "MsgN('Hi')" local func = CompileString(code, "TestCode") if func then -- Compile String returns nil if 3rd argument is true and code has errors. func() end
Output: Hi


Compiled code with custom arguments; captured with the varargs identifier.

local code = <page> local args = { ... } print( unpack( args ) ) print( args[ 2 ] + args[ 3 ]) print( args[ 4 ] .. args[ 5 ]) local first, second = ... print( first, second ) </page> local func = CompileString( code, "VarargCodeTest" ) func( 1, 2, 3, "A", "B", "C" )
1 2 3 A B C 5 AB 1 2

Page Links

Special Pages



Render Time: 84ms

DB GetPage 4
Generate Html 15
SaveChanges (1) 41
Render Body 0
Render Sidebar 21