How to convert a string into a calculation

How would i convert a string into a mathematical expression ?

local str = "1+1"
print(tonumber(str)) --Would print 2

I already tried the loadstring() function but does not seem to work

In such case, Roblox automatically will detect it is a mathematics operation, although it is assigned as string. You are supposed to get 2 here, if I understood you right.

I dont see any issues here, doesnt it print 2? It is supposed to

stringtoload = [[local str = 1+1
print(tonumber(str))]]

Then you would load that string using loadstring(stringtoload)
If you want to run the loaded string put closed parenthesis behind it. loadstring(stringtoload)()

Or alternatively you could do something like

local equation = "1+1"
stringtoload = "local str = " ..equation.. " print(tonumber(str))"

It prints nil because “1+1” is not an int or a float

Oh, my bad.
I perhaps thought it was the case where you did “1” + “1”.

@HeyWhatsHisFace 's reply would help then.

Would you be sticking to single operations per string or would you want it to solve multiple?

ex. “1+1” or “3-5+4”

I never really used loadstring() but i copied your script and says “not callable”

Since you converted str into a number by using tonumber, just convert it back into a string using tostring:

local str = "1+1"
print(tostring(tonumber(str)))

Do you have loadstring enabled in ServerScriptService?

I would prefer to solve multiple because it’s more useful

This will just convert nil into the string “nil”

local equation = "1 + 1"

local solution = loadstring("return "..equation)()

Yes i do have loadstring enabled

local equations = {"1+1", "2+2",}
local equationstring = "{ "
for i,v in ipairs (equations) do
	equationstring = equationstring .. " " ..v.. ","
end
equationstring = equationstring .."}"
stringtoload = "local equations = " ..equationstring.. " for i,v in ipairs (equations) do print(tonumber(v)) end"

I would start off with just single operations per string… then expand from there. You would need to use the string library to separate the components of the string out. Your issue is locating the operators in the string then finding the numbers between those operators… that I’m unsure how to do in the time given.

I would go against loadstring()… it makes for a quick way to make this work but could give easy access to the server if this has any client side communication which I would assume a calculator like this would have.

1 Like

Note: Don’t ever use loadstring just for this. There are better and safer ways.

1 Like

Thanks, that works but only in Roblox Studio, i don’t know if i can have one that works on compilers on other websites or something

Checking the integrity of the input is more efficient than writing your own math intepreter.

I know that i can use tables and split the string but that would be a very long script for something very simple as just converting a string into a mathematical expression

I guess the question is why are you trying to perform math inside of a print() in the first place. It is bad coding practice to use a print to perform math. print() is commonly used to help debug code.

local str = 1+1
print(str)