Using math operators as arguments/parameters in functions

Hello! I am making a game at the moment and I was wondering if there was any way to simplify my code. Currently I am using an if statement for checking if the calculation needs to be additive or substractive, I was wondering if maybe this could be done in a simpler way, without the need of an if statement ?

the way I’m doing it at the moment is:

local answer

local function Calculate(number1, operator, number2)
	if operator=="+" then
		answer = number1+number2
	elseif operator=="-" then
		answer = number1-number2
	end
end

But I’m wondering if maybe something like this would be possible?:

local answer

local function Calculate(number1, operator, number2)
	answer=number1 operator number2
end
1 Like

Probably not without using something like loadstring, but I wouldn’t recommend it as it can be a potential security issue and lack most luau optimizations. In your case, why would you need a function?

3 Likes

You could make an enum-like system where you assign certain operators certain numbers, but honestly, the way you’re handling it right now is fine.

2 Likes

Unfortunately, Luau doesn’t have a switch statement that one can use in a situation like this (I wish it did :slight_smile::+1:), so as a workaround, one method you can use is to create a dictionary that contains functions for each operator, and have the key be the operator, like so:

local calc = {
	['^'] = function(num1, num2) return num1^num2 end,
	['/'] = function(num1, num2) return num1 / num2 end,
	['*'] = function(num1, num2) return num1 * num2 end,
	['+'] = function(num1, num2) return num1 + num2 end,
	['-'] = function(num1, num2) return num1 - num2 end}

local function Calculate(number1, operator, number2)
	return calc[operator](number1, number2)
end

It’s not an ideal solution as a switch statement would be though, since it comes at a cost of an extra function call. If that is undesirable, then you should continue using if statements as you’re currently doing

4 Likes

As @OniiSamaUwU said you need an interpreter, you can use the luau interpreter built into the engine using loadstring, no other way other than that.

Or you can make your own if you want to include other things like parenthesis and be more advanced.

2 Likes

I see, that sucks, what security issues could it produce though? I’m curious, also I need to use a function called BuyUpgrades for buying any upgrade by just giving it parameters so it makes it very easy to make new upgrades

loadstring() in itself isn’t damaging, it’s how you use loadstring() that can be the issue, and some people may overlook that. Besides, there are other better ways on making an interpreter other than loadstring().

Say you have this (extremely basic example) code here that the client can run via RemoteEvent, passing in their own string from a TextBox as an example:

--!strict

local function someRemoteEventCallback(interpret: string): number
	return (loadstring(`return {interpret}`) :: any)() 
end

local interpreted: number = someRemoteEventCallback("2 * (3 + 4)")
print(interpreted)

If you’re hyper focused on the working aspect of it and fail to realize the security flaw, the client now has a way to pass in what they want to break your game.

local interpreted: number = someRemoteEventCallback("game.Players:ClearAllChildren()")

Now they can do what they want - possibly even require a backdoor and get full access to your server.

2 Likes