How to convert a string into a calculation

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.. ","
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

Why would you want to use a LuaU script externally from Roblox? Are you writing an exploit?

What if i want to use string.rep("+1",100) to add 100 ?

The issue is that what you are asking for is not as simple as you think it is. You have one data type and are trying to convert that into a datatype that does not easily convert…

You mean doing print((x^2 + y^2)^.5?) is bad? print is used to print the value of something, thus it’s also used to debug code.

Oh because loadstring() is a LuaU function ?
Also if i was writing an exploit i wouldn’t ask help on this forum

No. LuaU inherits the loadstring function from Lua. You can use the same function in any Lua compiler.

loadstring is not a Luau exclusive function, its inherited from Lua

Despite everyone warning you about not using loadstrings, if you know a thing or two about function environments you can make it somewhat “unexploitable”

To answer your original question, I have a thread that explains exactly what you want to do:

1 Like

To be honest i’m trying to do my thing for 6 hours and i’m pretty tired, thanks for helping but someone gave me the solution :+1:

I thought it might be a vaguely interesting thought exercise to actually do it algorithmically. Turns out it’s pretty boring since string iteration and indexing isn’t really a thing in lua. I also could have, but couldn’t be bothered to implement pemdas logic:

function calc_from_string (str)
	local start = tonumber(string.match(str,"%d+"))
	local operations = {["+"]=0, ["-"]=0}

	for i in string.gmatch(str,"%p%d+") do
		if string.sub(i,1,1) == "-" then
			operations["-"] -= tonumber(string.sub(i,2,string.len(i)))
		elseif string.sub(i,1,1) == "+" then
			operations["+"] += tonumber(string.sub(i,2,string.len(i)))
	return start+operations["+"]+operations["-"]

print(calc_from_string("100+20-1")) -- prints 119

I also realise now that this is probably not what you were looking for.

If you don’t want to use loadstring you could also call an api like with HttpService. The easiest code to write is the code someone else wrote :slight_smile:

Otherwise, parse the string into tokens and implement it yourself. I’m sure there’s a library out there that already does this that you could borrow/port.


Please share that solution here and/or mark it as the answer so others can find it!

1 Like