Note: Don’t ever use loadstring just for this. There are better and safer ways.
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)
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:
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
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
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