Script is returning nil with math

Here i’m telling the script to print me the answers to this question, but as i adjust it while calling the function it gives me the result AND nil or nil AND the result

local function Multiply(num, num2)
	print(num, num2)
end

Multiply(4 + 4)
Multiply(6 * 9)
Multiply(10 - 6)
Multiply(32 / 98)

image

You need to have a comma between the numbers, like

Multiply(4,4)
Multiply(6,9)
Multiply(10, 6)
Multiply(32, 98)

Currently, the game is computing the results of the arithmatic before calling the function, so you are basically just doing:

Multiply(8)
Multiply(54)
Multiply(4)
Multiply(0.326530612)

which is why you are getting that specific output.

2 Likes

Thank you for you r feedback, i just have one question though, what if i want to have my results be different for example one is a multiplication, one is a division, one is an addition.

if i put a comma between my numbers wouldn’t i need to clarify what arithmetic sequence id like to precede with in the print statement above??

Yeah.

You would either need to have dedicated functions for each operation (So Multiply, Divide, Add, Subtract), or you could put a single parameter, which would be a string containing the stuff you want (e.g. “3 + 5”) then process it further from there. That’s a tad more complicated though.

If you want help with each of those methods, let me know!

1 Like

Yes please some help would be great!! :slight_smile:

Which method would you like more help on? Seperate functions or passing a string?

1 Like

Both please, if you have the time but lets start with the string first since i dont know much about it

I’ll quickly describe the first method, given you want both, as it is way simpler. In the post after this one, I’ll outline the second method.


You would basically just create functions like this:

local function Add(num1, num2)
	return num1 + num2 
end

local function Subtract(num1, num2)
	return num1 - num2
end

local function Multiply(num1, num2)
	return num1 * num2
end

local function Divide(num1, num2)
	return num1 / num2
end

Add(4, 4) -- 8
Multiply(6, 9) -- 54
Subtract(10, 6) -- 4
Divide(32, 98) -- 0.326530612..

Note that this doesn’t actually output the values yet. Within the functions, I’ve made use of return which will send the value after return ‘back’ to wherever the function was called. This means that you can then store the result to a new variable, or print it out easily:

local Result = Add(5,6) -- Result then has a value of 11.
print(Add(9, 1)) -- Outputs 10
1 Like

You’ll utilize string patterns for this. Even this solution can’t handle more than binary operations

type Transforms = {
    [string]: (number, number) -> number	
}


local NUMBER  : string = "-?%d+%.?%d*"
local PATTERN : string = `^ *({NUMBER}) *(.-) *({NUMBER}) *$`

local TRANSFORMS : Transforms = {
    ["^"]  = function(a, b) return a ^ b end,
    ["*"]  = function(a, b) return a * b end,
    ["/"]  = function(a, b) return a / b end,
    ["//"] = function(a, b) return a // b end,
    ["%"]  = function(a, b) return a % b end,
    ["+"]  = function(a, b) return a + b end,
    ["-"]  = function(a, b) return a - b end,
}


local function process(input: string): number
    local left, operator, right = string.match(input, PATTERN)
    if not left then
        error("Unexpected input format.")
    end

    local transform = TRANSFORMS[operator]
	if not transform then
        error("Unknown operator: " .. operator)
	end

    return transform(
		tonumber(left), 
		tonumber(right)
	)
end


print(process("5 + 5")) --> 10

i’m new to coding this block of code genuinely made me flinch :sob:

Check again. I forgot to consider some cases :rofl:

1 Like

but thank you nevertheless any help is better than no help

Now for the string method.
I’m not sure how familiar you are with coding, so I’ll provide a basic explanation:
A string is basically just text, and is surrounded by quotations mark (there are also alternative things to surround it with).

In

local Text = "HELLO"

the variable Text gets assigned the value HELLO, which is a string.


For simplificities sake, the code outlined in this post can only carry out a single operation in the text (e.g. "5 + 3") and not multiple, such as "2 + 5 + 7")

The first step is just to define the function, so:

local function Calculate(Text)

end

In terms of actually processing Text, I would first recommend getting rid of any space characters ( ), as they don’t add any information. This can be achieved using gsub of the string library. (gsub is basically just a replace function)

local function Calculate(Text)
	local Text2, _ = string.gsub(Text, " ", "") 
	-- Replace all occurances of " " with ""
	-- which basically just removes them
	-- If Text = "5 + 3", Text2 will = "5+3"

	-- Note the _ after 'Text2,'. This is basically saying to get the value obtained, but disregard it.
	-- This is needed as gsub returns 2 values - the new string, plus the number of changed values.
	-- As we don't care about how many were changed, we disregard it.
end

Next, I would check which operation is found within the text, using find of the string library. Find will return the position of the character in the string. We only care that it is found though.

local function Calculate(Text)
	local Text2, _ = string.gsub(Text, " ", "") 
	-- Replace all occurances of " " with ""
	-- which basically just removes them
	-- If Text = "5 + 3", Text2 will = "5+3"

	-- Note the _ after 'Text2,'. This is basically saying to get the value obtained, but disregard it.
	-- This is needed as gsub returns 2 values - the new string, plus the number of changed values.
	-- As we don't care about how many were changed, we disregard it.

	if string.find(Text2, "+") then
		-- Do the addition stuff
	elseif string.find(Text2, "-") then
		-- Do the subtraction stuff
	elseif string.find(Text2, "*") then
		-- Do the multiplication stuff
	elseif string.find(Text2, "/") then
		-- Do the division stuff.
	else
		warn("Unknown operation")
		-- This outputs "Unknown operation", in orange text!
	end
end

Then, we need to get the relevant numbers. We can do this using split of the string library, which seperates the string at a specified point.

local function Calculate(Text)
	local Text2, _ = string.gsub(Text, " ", "") 
	-- Replace all occurances of " " with ""
	-- which basically just removes them
	-- If Text = "5 + 3", Text2 will = "5+3"

	-- Note the _ after 'Text2,'. This is basically saying to get the value obtained, but disregard it.
	-- This is needed as gsub returns 2 values - the new string, plus the number of changed values.
	-- As we don't care about how many were changed, we disregard it.

	if string.find(Text2, "+") then
		-- Do the addition stuff
		local Numbers = string.split(Text2, "+")
		local Num1 = Numbers[1] -- Get the first number
		local Num2 = Numbers[2] -- Get the second number

	elseif string.find(Text2, "-") then
		-- Do the subtraction stuff
		local Numbers = string.split(Text2, "-")
		local Num1 = Numbers[1] -- Get the first number
		local Num2 = Numbers[2] -- Get the second number

	elseif string.find(Text2, "*") then
		-- Do the multiplication stuff
		local Numbers = string.split(Text2, "*")
		local Num1 = Numbers[1] -- Get the first number
		local Num2 = Numbers[2] -- Get the second number

	elseif string.find(Text2, "/") then
		-- Do the division stuff.
		local Numbers = string.split(Text2, "/")
		local Num1 = Numbers[1] -- Get the first number
		local Num2 = Numbers[2] -- Get the second number

	else
		warn("Unknown operation")
		-- This outputs "Unknown operation", in orange text!
	end
end

Finally, we can do the relevent operation and return the result:

local function Calculate(Text)
	local Text2, _ = string.gsub(Text, " ", "") 
	-- Replace all occurances of " " with ""
	-- which basically just removes them
	-- If Text = "5 + 3", Text2 will = "5+3"

	-- Note the _ after 'Text2,'. This is basically saying to get the value obtained, but disregard it.
	-- This is needed as gsub returns 2 values - the new string, plus the number of changed values.
	-- As we don't care about how many were changed, we disregard it.

	if string.find(Text2, "+") then
		-- Do the addition stuff
		local Numbers = string.split(Text2, "+")
		local Num1 = Numbers[1] -- Get the first number
		local Num2 = Numbers[2] -- Get the second number

		return Num1 + Num2

	elseif string.find(Text2, "-") then
		-- Do the subtraction stuff
		local Numbers = string.split(Text2, "-")
		local Num1 = Numbers[1] -- Get the first number
		local Num2 = Numbers[2] -- Get the second number

		return Num1 - Num2
	elseif string.find(Text2, "*") then
		-- Do the multiplication stuff
		local Numbers = string.split(Text2, "*")
		local Num1 = Numbers[1] -- Get the first number
		local Num2 = Numbers[2] -- Get the second number
		
		return Num1 * Num2
		
	elseif string.find(Text2, "/") then
		-- Do the division stuff.
		local Numbers = string.split(Text2, "/")
		local Num1 = Numbers[1] -- Get the first number
		local Num2 = Numbers[2] -- Get the second number
		
		return Num1 / Num2

	else
		warn("Unknown operation")
		-- This outputs "Unknown operation", in orange text!
	end
end

You can then call the function like this:

Calculate("5 + 3") 
Calculate("7 * 1")
Calculate("10 / 2")

You can store the result to a variable

local Result = Calculate("7 * 3")
-- Result will have the value 21

or just output it directly:

print(Calculate("10 / 2")) 
-- Outputs 5

Technically, you should probably do tonumber() on Num1 and Num2

local Num1 = tonumber(Numbers[1])
local Num2 = tonumber(Numbers[2])

However it will actually work without it, as the compiler recognises that they are both numbers

1 Like

Thank you so much for your time/help, you’ve saved me a tremendous amount of research and i cant thank you enough. I will take what you’ve given me and study it so later as a programmer i can use or tweak it. Once again thank you

Feel free to reach out if you need further assistance!

Also, make sure to mark the reply as the solution if it solved your problem!

1 Like