I am trying to create a console in my game so that my Admins are able to execute 1 Line scripts but i have literally no idea how to run a script that is stored in a textbox or a variable
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;
local code = "print('hello world')" local compiled_code = loadstring(code) compiled_code() -- this should cause the code to run
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:
local code = "print('hello world')" local compiled_code,err = assert(loadstring(code)) if(compiled_code) then compiled_code() else print("error:",err) end
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