I'm making a gui calculator, how to convert string to operators?

My issue is I can’t do a calculation when I do print(“10+5/5”) , it will just print out “10+5/5”.
The numbers calculated is based on user input.

How do I make it so I can convert my string to perform calculation?

2 Likes

You can wrap it a loadstring, but keep in mind only server scripts can use it and ServerScriptService.LoadStringEnabled has to be on.

print(loadstring("return 10 + 5/5")())

will get you the result.

2 Likes

Maybe you can try to use tonumber().

Do you have any other way in mind without using loadstring? I’m afraid this will pose a security threat.

@Intencidy To number doesn’t work.

Another method is to use a dictionary storing functions:

--Translate string into mathematical expression
local prefixesFunctions = {
    ["/"] = function (number1,number2) 
        return number1/number2 
    end;
	["-"] = function (number1,number2) 
        return number1 - number2 
    end;
	["*"] = function (number1,number2) 
        return number1*number2 
    end;
	["+"] = function (number1,number2) 
        return number1+number2 
    end;
}

From this other post, with a similar gui calculator project:

It won’t pose a security threat if you don’t trust the client by sandboxing properly. You just want to calculate expressions, so you wouldn’t really need any global environment.

The code I have so far is below. The buttons are named “Button1”, “Button2”, each corresponds to the number.

Please let me know how I can apply that dictionary into here.


for i,v in pairs (calculatorFrame:GetChildren()) do 
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			local currentText = calculatorFrame.TextCalculation
			local actionToDo = string.gsub(v.Name, "Button", "")
			if tonumber(actionToDo) ~= nil then
				currentText.Text = currentText.Text..tostring(actionToDo)
			elseif actionToDo == "Plus" then
				currentText.Text = currentText.Text.."+"
			elseif actionToDo == "Minus" then
				currentText.Text = currentText.Text.."-"
			elseif actionToDo == "Multiply" then
		--		if not string.find(
				currentText.Text = currentText.Text.."*"
			elseif actionToDo == "Divide" then
				currentText.Text = currentText.Text.."/"
			elseif actionToDo == "Calc" then -- calculate the whole string
				
			end 
		end)
	end
end

This might be helpful:

Thank you, but I’ve already looked into this.

Okay… did it not work or…? What do you need to do that loadstring or this module can’t cover?

Behold:
https://www.roblox.com/library/5005473860/Luaxp-Expression-Parser

1 Like

I prefer not to use loadstring or modules, just a few lines of script if possible.

That is what loadstring can help you achieve, it’s not inherently dangerous.

1 Like

Do you have a link to the Roblox documentation page of loadstring? I couldn’t find anything. Thanks.

https://developer.roblox.com/en-us/api-reference/lua-docs/Lua-Globals

It only works server-side so exploiters can’t access it unless you have unsecure remote events

Sample using the parser:

local parser = require(5005473860)

local expression = parser.compile("9/3+7")

local result = parser.run(expression)

print(result) -- 10

--OR

print(parser.evaluate("asin(1)*2")) -- pi

Sample using loadstring:

local expression = "math.exp(1)"

local f = loadstring("return "..expression)

setfenv(f, {math = math}) -- IMPORTANT TO PREVENT CODE INJECTION

local result = f()

print(result) -- e
1 Like

Thanks, it works great. Does your paser thing have a documentation?

I got the code from here:

1 Like

Do you know how would I be able to find the square root and square number of x? I tried using ^ but it works like an addition, instead of exponent.

Unfortunately from reading through the source code it looks like the ^ symbol was only programmed in as a bitwise ‘xor’ operation. For exponents and square roots you would have to use pow(2, 3) and sqrt(9)