Answered by
5 years ago Edited 5 years ago
well, Roblox already has an in game console which you can open by pressing f9
but you can make your own script executor like this:
also, make sure the code runs in a server script/normal script or a module script that's called by a server script, because the function to turn string into executable code is blocked in LocalScript or Module Scripts that invoke by LocalScript, which means your going to need remote events to transfer text from player to server;
1 | local code = "print('hello world')" |
2 | local compiled_code = loadstring (code) |
so the above code stores valid lua code in the variable code
;
then calls a lua built-in function named loadstring
.
loadstring
returns another function which you can call to run the code, in this case, the function is stored in the variable compiled_code
the code doesn't help in catching errors, but you can catch errors with assert
like this:
1 | local code = "print('hello world')" |
2 | local compiled_code,err = assert ( loadstring (code)) |
if i remember right, i believe, if the code compiled successfully then the first value that assert
returns is the value that the function that was provided to it returned, in this case its the functiion to run the code, otherwise, it returns nil, and another value which is just a string of what went wrong