Running code IN a variable

I’m trying to create a system where players can create a “if then statement” through text. I turn said text into a variable, but if I attempt to run it, it dosent work.

as an example;

TextInput = “workspace.Baseplate.Transparency == 0”

if TextInput then
print(“working”)
end

How do I get the variable to be read out as if it was normal code? Or does it require another method?
I would love some help on this, as I’m currently trying to create a “Neural Network Engine”. Its basicly just a set of if then statements that form an AI. Anyone could use it, so I needed the code to be typed into a textbox, but it just dosent read it like code, and still reads it like a variable.

1 Like

You can enable Loadstring in ServerScriptService, and at that point you could probably do-

local condition = "workspace.Baseplate.Transparency == 0"
local statement = loadstring(condition)

However, loadstring appears to be deprecated and not recommended for use unless absolutely necessary, as with it enabled players can potentially execute code via exploits to screw stuff up, I don’t know much about that but I saw it on a post.

Hope this helps.

2 Likes

When testing this, it appears the Statement just sends in nil, or perhaps Im doing it wrong, either or I’ll keep checking for solutions.

I wondered if that would happen. I guess loadstring doesn’t actually return anything; it just executes the code.

You could use string.split or string.sub to divide the text input. For example, if there is a dot in the text you could check if the fragment of string exists as an object and if it does, index stuff on it.

I hope you get the idea; I did not elaborate because this is a very expensive method and there are probably other alternatives much more efficient…

If you know what you’re doing, there is no problem on using loadstring. However, you should check that the client code only contains if statement (and not other stuff like functions).

1 Like

I remember seeing a post about this a long while ago, but the premise was someone got a lua bytecode interpreter which read the ‘string’ you fed it and it ran it as lua bytecode

this doesn’t really relate to your context but it runs code that is sent to it as a string (you can use multiline strings here). Here is a thread asking how the adonis :s command works which runs scripts from a users string How does the :s command in Adonis work?

It compiles and doesn’t run the code. It’s returning nil because workspace.Baseplate.Transparency == 0 is not a valid Lua statement. Lua expressions can’t be standalone; you’d need to insert a return at the start.

2 Likes

h = loadstring(“return workspace.Baseplate.Transparency == 0”)

if h then
print(“test”)
end

thats the testing code I used for this, and it APPEARS to work fine.
(I say appears because sometimes roblox has a stroke and stops working with me)

Anywho this seems to have fixed my issue so Thank you very much. I’ll mark it as the solution unless I come across an Error in more code.

You still need to call the function. h is now a function, that when run, executes your code.

You also need to handle the case in which the code is still not valid, like when the user inputs a bunch of symbols. In this case, loadstring returns nil followed by an error message.

I got that part kinda already figured out, I was actually planning on adding a cool sci fi explosion if the code is invalid for fun. Thanks for the advice though, I really would have no clue what to do without dev forum.