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