Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to create a ingame console?

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

1 comment

0

Try free-modelling?

1 answer

✓ Accepted answer

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

4 comments

0

loadstrings have to run on the server you have to use a remote event

0

also do you know if you can make an output?

0

Wow nice response

0

thx

This discussion is preserved as it was. New answers, comments, and votes are closed.