Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to create a ingame console?

Asked by 5 years ago

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

0
Try free-modelling? NIMI5Q -2 — 5y

1 answer

Log in to vote
2
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;

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

0
loadstrings have to run on the server you have to use a remote event danglt 185 — 5y
0
also do you know if you can make an output? danglt 185 — 5y
0
Wow nice response Fad99 286 — 5y
0
thx User#23252 26 — 5y
Ad

Answer this question