of     1   

Greendolph
#44912307Friday, April 01, 2011 3:57 AM GMT

I am trying to make a gui that allows players to do basic arithmetic in a text box. This is hte script I have so far- but its not working. It attempts to use loadstring to set a variable. function click() text = script.Parent.Parent.Text --sets the value 'text' if string.sub(text,1,6) == "print(" then --if player uses print function loadstring("x = " .. string.sub(text, 7, string.find(text, ")")- 1)) --set x to value of string between ( and ), so "print(2 + 4)" is 6. script.Parent.Parent.Output.OPVal.Value = x -- set int value to x end end script.Parent.MouseButton1Click:connect(click)
LPGhatguy
Forum Moderator
#44913657Friday, April 01, 2011 4:37 AM GMT

Wouldn't it just be easier to execute the code in a limited Lua environment, and return whatever was written in? Here's something more functional than what you have. local MathEnv = { math = math } function Execute() local Source = "return (" .. script.Parent.Parent.Text .. ")" local Lua = loadstring(Source) if (Lua) then setfenv(Lua, MathEnv) script.Parent.Parent.Output.OPVal.Value = tostring(Lua()) end end script.Parent.MouseButton1Click:connect(Execute)

    of     1