Calculating math equations with tables

  1. What do I want to achieve?
    I can add a symbol like + - * / to a table and the code will recognize it as a symbol of math.

  2. What’s the issue?
    Currently I can’t figure out a way to get Roblox to recognize it as a simple equation.

  3. What solutions have you tried so far?
    I’ve tried searching up on Google, either not finding anything or a way to explain it.

If you have any ideas, please comment them below, have a nice day and thank you for reading!

You can use a lookup with the signs as their keys

local equations = {
   ["+"] = function(a, b)
      return a + b
   end,
   ["-"] = function(a, b)
      return a - b
   end
}

if isFirstNum and IsSecondNum and equations[sign] then
   local product = equations[sign](firstNumber, secondNumber)
end
2 Likes

Thank you so much, your way is actually genius as hell, thank you!!!

1 Like
local function calc(x, y, sign)
	return loadstring('ret = '.. x..sign..y..' return ret')()
end

local result = calc(11,22,'+')
print(result)
local result = calc(11,22,'*')
print(result)

if you use loadstring…

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.