How to convert a string into a calculation

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)))
		end
	end
	
	return start+operations["+"]+operations["-"]
end

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 https://api.mathjs.org/ 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.

Also

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

1 Like

Itā€™s a string datatype. A string is just plaintext. Roblox doesnā€™t interpret anything in strings automatically, youā€™d need to do that yourself. The output would say ā€œ1+1ā€ and nothing else.

I know Iā€™m a bit late and Iā€™m sorry :stuck_out_tongue: but since this isnā€™t solved yet maybe try to enable loadstring in the server script

image

then do this:

local algo = assert(loadstring("return " .. Calculation)) -- this line compiles string into a function
local Answer = algo()
print(Answer) -- prints out the answer :P

if you want an explanation on what that block of code does lmk

do note that loadstring might not work on local scripts and only server scripts and loadstring isnā€™t that reliable

According to me, ā€œ1+1ā€ isnā€™t counted as a number, and using tonumber() on it wouldnā€™t work as it would return nil. Correct me if Iā€™m wrong.

tonumber() will only return a real number not an expression/calculation

this should work ig
i used gpt to get these 3 variables only
local left, op, right = expression:match(ā€œ(%d+)([%+%-%*/])(%d+)ā€)

the code

local function StringToExpression(expression)
	local left, op, right = expression:match("(%d+)([%+%-%*/])(%d+)")
	if left and op and right then
		left, right = tonumber(left), tonumber(right)
		if op == "+" then return left + right
		elseif op == "-" then return left - right
		elseif op == "*" then return left * right
		elseif op == "/" then return left / right
		end
	end
end


local str = "1+1"
print(StringToExpression(str)) -- prints 2
print(StringToExpression("100/2")) -- prints 50

this would work on any string if its like ā€œ1+1ā€ if its ā€œ1+ 1ā€ then willnot work
also the function is optimized and you can make it --!native so it runs better

also i donot recommend enabling loadstring becaues itsnot safe

check the ServerScriptService documentation before using it
ā€“ServerScriptService | Documentation - Roblox Creator Hub