I am stuck trying to make a calculator project

I’m new to roblox scripting, so I am currently stuck on making a simple calculator looking like this:


What should I do next?

local numButtons = script.Parent.Frame.NumButtons:GetChildren()
local plus = script.Parent.Frame.Plus
local minus = script.Parent.Frame.Minus
local multiply = script.Parent.Frame.Multiply
local divide = script.Parent.Frame.Divide
local text = script.Parent.Frame.TextLabel
local totalValue = script.Parent.Frame.NumButtons.TotalValue


for i, num in pairs(numButtons) do
	if not num:IsA("IntValue") then
		numButtons[i].MouseButton1Click:Connect(function()
			print("works")
			text.Text = numButtons[i].Text
		end)
	end
	
	plus.MouseButton1Click:Connect(function()
		print("works")
		
	end)
end


2 Likes

Are you actually having a scripting related problem or…?

As the lastest reply said, are you wanting help to fix the calculator or just wanting help to get new ideas for it?

I wanted suggestions from you guys on how i should make a simple calculator, since I’m stuck

On how to make it? You already did!

Do you mean that do you want new ideas for new buttons?

No I’m actually stuck on the scripting part like I don’t know i’d script to return value from a calculator after it’s being added, subtracted, multiplied, divide etc. So it’s a scripting related problem

First, set it up so that punching in numbers will adjust some variable, lets call x1.
So pressing 1,2,3,4 will change the variable to 1,12,123,1234 respectively. You can do this so when a button is pressed, multiply this variable by the base (10), then add the number pressed, then display this new value on the screen.

Then when an operator button is hit (add, subtract, etc), remember that operator and move the value of x1 to a different variable x2. Clear x1 to 0. After the user has input another number and presses the equals button, you can perform the operator on both x1 and x2 to get the result.

I’m sorry How would I do this? Can you show me what you’re talking about on a script? I’m new to Roblox scripting

I made a calulator game that is open sourced (Calculator - Roblox)

Look through it to get a general idea (I forget what the scripts look like so sorry if they’re messy)

I see what you mean. So, if I am correct the simplest way to make the buttons work would be this:

local numButtons = script.Parent.Frame.NumButtons:GetChildren()
local plus = script.Parent.Frame.Plus
local minus = script.Parent.Frame.Minus
local multiply = script.Parent.Frame.Multiply
local divide = script.Parent.Frame.Divide
local text = script.Parent.Frame.TextLabel
local totalValue = script.Parent.Frame.NumButtons.TotalValue


for i, num in pairs(numButtons) do
	if not num:IsA("IntValue") then
		numButtons[i].MouseButton1Click:Connect(function()
			print("works")
			text.Text = text.Text .. numButtons[i].Text
		end)
	end
	
	plus.MouseButton1Click:Connect(function()
		print("works")
		
	end)
end


1 Like

Basically, what I am doing there is concatenating (combining) the old text with the new text.

He wants to make the buttons work, not just text input.

Alright so how would I make the script to add two different numbers when the add button is being clicked

Alright so, there’s this thing called string:split(“+”). You can use it whenever a person presses enter. This function returns all the substrings that have been split by the character you inputted in the text.Text object.

you should use a few number variables
I would have atleast one for input 1 one for input 2 and then 3rd which is the sum of those two added subtracted etc…
and 1 varaible for your operator like + - or /

then push them to the text like Text = Input1 … Operator … Input2 … ‘=’ … Sum

you can add some spaces in there if needed

That could work. but the problem with that is that unless you use a dictionary, the amount of arguments will be limited

you could just use a table in that case if you wanted multi numbers say like 1+5+3+8
just use table and reference by its key in order of how they are put in calculator
same with an operator talbe

on a base calc thought it doesn’t have to have that much going on

So what would it look like on a script?

you will need to setup the = to calculate them to your sum with a function but you could setup something like this below:

you can also make it a lot more complex in the way its coded and functions it does

local numButtons = script.Parent.Frame.NumButtons:GetChildren()
local plus = script.Parent.Frame.Plus
local minus = script.Parent.Frame.Minus
local multiply = script.Parent.Frame.Multiply
local divide = script.Parent.Frame.Divide
local text = script.Parent.Frame.TextLabel
local totalValue = script.Parent.Frame.NumButtons.TotalValue

local Input1
local Input2
local Operator
local Sum


function UpdateText()
	local temptext = ''  -- empty text
	if Input1 then
		text.Text = Input1
		if Operator then
			text.Text = Input1 .. Operator
			if Input2 then
				text.Text = Input1 .. Operator .. Input2
				if Sum then
					text.Text = Input1 .. Operator .. Input2 .. '=' .. Sum
				end
			end
		end
	else 
		text.Text = ''  -- no input one so reset the text empty
	end
end


for i, num in pairs(numButtons) do
	if not num:IsA("IntValue") then
		numButtons[i].MouseButton1Click:Connect(function()
			print("works")
			if not Input1 then
				Input1 = tonumber(numButtons[i].Text)  -- changes the text into number for the varaible
			elseif not Input2 then
				Input2 = tonumber(numButtons[i].Text)  -- changes the text into number for the varaible
			end	
			UpdateText()
		end)
	end

	plus.MouseButton1Click:Connect(function()
		print("works")
		Operator = '+'
		UpdateText()
	end)
end
1 Like

Thank you and thanks to everyone who were helping me here. Appreciate it.