Run code from an input?

Is there anyway to run code from an input such as a text box? It will be used for simple tasks from the client side

3 Likes

You can make a:GetPropertyChangedSignal() event and focus it on “Text” which would be used for when you want the event to be fired every time you add or remove Text.
OR
You could make an event for when you’re finished typing. You can use .LostFocus event to do this.

1 Like

If you want to execute code from a string on the server, you can use the loadstring() function, although it’s usually a bad practise and should be avoided.

If you want however to run code from a string on the client, then it isn’t really possible. The only way you could do that is create a dummy account and some API endpoint which will create a new model with a LocalScript with the desired source on the account and then return its id so you can load it in game.

Edit: didn’t mean to reply to you @uJordy, sry

1 Like

aha, that’s alright

1 Like

I was mostly intrigued at how this game was able to accomplish it https://www.roblox.com/games/1334669864/Lua-Learning-WIP I want to replicate something similar to it

1 Like

Can you be more specific? Are you talking about the code block?

1 Like

If we take this game as an example https://www.roblox.com/games/1334669864/Lua-Learning-WIP it has a sort of system to execute code like a script in roblox studio

1 Like

I would assume it would be done through a module of some sort which can deal with the input and use string manipulation to put it as the output:


i.e in this case it find the quotes and gets the string in between

3 Likes

Oh I see, so a module would have to contain some sort of function for every scenario?

1 Like

That’s one way to do it I guess yes. Or, when it finds the string “print” it knows it needs to find two quotes so it can go to the output. Same thing for when you do a function or what not

1 Like

I don’t think that’s what it does ;p

Picture

As you can see, it ran a string.char code just fine. So it does either of these:

  • Runs the code on the server and sandboxes it
  • Uses a lua parser like einsteinK’s one
2 Likes

Thats interesting, I’ll look into it

1 Like

I suppose he uses a lua parser, since it looks like you cannot access the game instance.

1 Like

How about using a Lua VM? Some people have already made some Lua VMs. Maybe not made but imported:


2 Likes

Feel free to fully read my answer before replying. I already mentioned two ways of doing this, one being a parser (aka vm) and other being uploading a LocalScript as a new model and then loading it.

The reason i said it isn’t really possible is because both of those ways have their limitations.
A parser aka a vm can produce unexpected results with complex logic, and uploading a new model each time is guaranteed to have at least 1 second delay, plus it runs as a new LocalScript (doesn’t return a function).

1 Like

Ah, alright I see Yes they all have limitations which is obvious but at least it is realizable.

1 Like

I haven’t actually played that game before. Are you sure they’re actually running code from text input? It could be simulated and the code can be checked through the standardised string library.

Regardless of what it’s doing, I personally would not recommend allowing the client to run code. What is your use case for allowing the client to run code and who would be able to access it?

Arbitrary code execution based on user input is an anti-pattern in software development. Try to avoid it if you can unless you are absolutely sure that you can prevent unauthorized code from executing (i.e. submitted by users that should not be able to do that). Even then, it’s probably more trouble than it is worth, and a few hardcoded commands with parameters may do the job just as fine.

1 Like

Sorry for the late response, my case for allowing the client to execute code is for them to have a simulated lua terminal, there won’t be anything too major for them to tinker with in the actual game. I will be putting some security measures in place.

1 Like