How to use math on a variable / variables?

  1. What do you want to achieve? Keep it simple and clear!

Hello, I’m trying to make a calculator for a fun challenge but I ran into a little bit of a problem.

  1. What is the issue? Include screenshots / videos if possible!

Basically when a player presses a button (for example 1) then I want to show 1 on the screen of the calculator!
Then the player presses a prefix and another number. This for example: (10/2) Now when the player presses the = button I want to divide 10 by 2 but I have no clue how to do that with variables! At line 39 is the problem (It’s under the huge if statement that checks if everything isn’t 0 and if the player pressed =.

local Frame = script.Parent.Parent.Parent.Numbers
local Buttons = Frame:GetChildren()
local Frame = script.Parent.Parent.Parent.Numbers
local Buttons = Frame:GetChildren()
local TL = script.Parent.TextLabel
local num1 = script.Parent.Number1
local num2 = script.Parent.Number2
local PrefixVal = script.Parent.Prefix

local prefixes = {
	"/";
	"-";
	"*";
	"+"
}

local enter = "="

local Numbers = {
	"0";
	"1";
	"2";
	"3";
	"4";
	"5";
	"6";
	"7";
	"8";
	"9"
}

while wait(0.1) do
	for i, v in pairs(Buttons) do
		v.MouseButton1Click:Connect(function()
			if prefixes[v.Name] then
				local prefix = prefixes[v]
				TL.Text = TL.Text .. prefix
				PrefixVal.Value = prefix
			end
			if v.Name == enter and num1.Value ~= 0 and PrefixVal.Value ~= "" and num2.Value ~= 0 then
				TL.Text = num1.Value PrefixVal.Value num2.Value --[[ line 39. U can see why there is a problem...  But when a player put in 2 numbers and a prefix why won't line 39 look like this: TL.Text = 10 / 5 ???  ]]--
			else
				-- Player didn't complete the input.
			end
			if Numbers[v.Name] then
				if num1.Value == 0 then
					num1.Value = v.Name
					TL.Text = v.Name
				else
					if num2.Value == 0 then
						num2.Value = v.Name
						TL.Text = TL.Text .. v.Name
					else
						print("Max numbers reached")
					end
				end
			end
		end)
	end
end

Is there a way of getting the number? Please tell me :D!

Can you instead tell us which line is line 39.

There is a comment next to line 39.

It’s under the huge if statement that checks if everything is not set to 0 and checks if the input is =

I believe the issue comes from the compiler not understanding what line 39 is as currently the compiler thinks it’s as weird syntax as I’m assuming the Values are string values from a text box GUI like so:

TL.Text = "num1 value" "num 2 value" "num 3 value"

Yeah, the machine compiler reading it is thinking of which variable to assign TL.Text with num1 value, num2 value, or num3 value and is yeah confused and will prompt an error.

To solve it we can use a function dictionary to translate a string value into the proper Lua mathematical syntax the compiler will understand what to do with.

local Frame = script.Parent.Parent.Parent.Numbers
local Buttons = Frame:GetChildren()
local Frame = script.Parent.Parent.Parent.Numbers
local Buttons = Frame:GetChildren()
local TL = script.Parent.TextLabel
local num1 = script.Parent.Number1
local num2 = script.Parent.Number2
local PrefixVal = script.Parent.Prefix

local prefixes = {
	"/";
	"-";
	"*";
	"+"
}

--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;
}

local enter = "="

local Numbers = {
	"0";
	"1";
	"2";
	"3";
	"4";
	"5";
	"6";
	"7";
	"8";
	"9"
}

while wait(0.1) do
	for i, v in pairs(Buttons) do
		v.MouseButton1Click:Connect(function()
			if prefixes[v.Name] then
				local prefix = prefixes[v]
				TL.Text = TL.Text .. prefix
				PrefixVal.Value = prefix
			end
            if v.Name == enter and num1.Value ~= 0 and PrefixVal.Value ~= "" and num2.Value ~= 0 then
                local number1 = tonumber(num1.Value)
                local number2 = tonumber(num2.Value)
                --Use the dictionary to find the correct mathematical syntax
                local answerNumber = prefixesFunctions[PrefixVal.Value](number1,number2)
				TL.Text = tostring(answerNumber) --[[ line 39. U can see why there is a problem...  But when a player put in 2 numbers and a prefix why won't line 39 look like this: TL.Text = 10 / 5 ???  ]]--
			else
				-- Player didn't complete the input.
			end
			if Numbers[v.Name] then
				if num1.Value == 0 then
					num1.Value = v.Name
					TL.Text = v.Name
				else
					if num2.Value == 0 then
						num2.Value = v.Name
						TL.Text = TL.Text .. v.Name
					else
						print("Max numbers reached")
					end
				end
			end
		end)
	end
end

This should translate string into lua mathematical expressions. Hopefully, it works haven’t tested it out.

1 Like