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