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.
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.
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…
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:
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
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!
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 but since this isn’t solved yet maybe try to enable loadstring in the server script
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