Hello.
I want to make a calculator on roblox just for fun and I am writing this to see what would be the most efficient way on doing it with the UI that I have already created!
How should I structure the script?
Hello.
I want to make a calculator on roblox just for fun and I am writing this to see what would be the most efficient way on doing it with the UI that I have already created!
How should I structure the script?
Well, you’d need to detect when the button is pressed and I’d store it in an int value or in the script.
Then with the first number you need to get an operator; when an operator is chosen hide the initial value the user typed out and ready it for another number to be typed in
whipping up a test script right now.
--Make Variables (Store These In The Script Through Number Value When You Start Modifying This And Then Reference Their Values Here)--
local NumberSetOne = 0
local NumberSetTwo = 0
local MathType = "None"
--Have A Function That Adds Numbers Together (Not Addition Just Adding To String)--
local function NumPressed(Num)
if MathType == "None" then
if NumberSetOne ~= 0 then
NumberSetOne = NumberSetOne..Num
else
NumberSetOne = Num
end
else
if NumberSetTwo ~= 0 then
NumberSetTwo = NumberSetTwo..Num
else
NumberSetTwo = Num
end
end
end
--Clears The Numbers--
local function Clear()
NumberSetOne = 0
NumberSetTwo = 0
MathType = "None"
end
--Function For When The Player Pressed "*" or "+" or "/" etc.--
local function SetMathType(T)
MathType = T
end
--Function For When The Player Hits "="--
local function PerformCalc()
if MathType == "Multiplication" then
NumberSetOne = NumberSetOne * NumberSetTwo
print(NumberSetOne)
elseif MathType == "Addition" then
NumberSetOne = NumberSetOne + NumberSetTwo
print(NumberSetOne)
elseif MathType == "Division" then
NumberSetOne = NumberSetOne / NumberSetTwo
print(NumberSetOne)
elseif MathType == "Subtraction" then
NumberSetOne = NumberSetOne - NumberSetTwo
print(NumberSetOne)
else
print("No MathType Set")
end
MathType = "None"
end
--Activating The Functions--
ClearGUIButton.Activated:Connect(Clear())
EqualsGUIButton.Activated:Connect(PerformCalc())
--Make As Many Of These As there Are Number Buttons
NumberButton1.Activated:Connect(NumPressed(1))
--Make As Many Of These As there Are Math Type Buttons
AdditionButton.Activated:Connect(NumPressed("Addition"))
Then you can use when the number values in the script change you can change the display through another script.
Also this script is untested so i cant tell you how well this works this was mostly a proof of concept and needs to be modified to be used.
Hello! Since I’ve seen this post (around 1-2 hours ago) I have started making a calculator from scratch to see if I can actually make one, I have color picked every color from the picture so the UI shouldn’t look bad. The model only contains one local script that you can check for anything you want, it’s not virused lol. Here it is: Calculator GUI - Roblox I hope I helped!
Also, if you would like to adapt a script to your already existing UI, that’s fine but you will have to change some variables and the names of the buttons and things. Here’s the source code anyways:
--Script Made by Sarpomijon11 14/10/2022
local MainFrame = script.Parent:WaitForChild("MainFrame")
local ButtonsHolder = MainFrame:WaitForChild("ButtonsHolder")
local ResultText = MainFrame:WaitForChild("Result")
local Symbol = MainFrame:WaitForChild("Symbol")
local FirstNumber = 0
local SecondNumber = 0
local SwitchToSecondNumber = false
local SymbolSelected
local MathFunctions = {
["/"] = 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/100*b end;
}
local Symbols = {
["="] = function()
if not (FirstNumber and SecondNumber) then return end
FirstNumber = tonumber(FirstNumber) ; SecondNumber = tonumber(SecondNumber)
local NumberResult = MathFunctions[SymbolSelected](FirstNumber,SecondNumber)
ResultText.Text = AddCommas(NumberResult)
FirstNumber = NumberResult
SecondNumber = 0
Symbol.Text = ""
end,
["AC"] = function()
FirstNumber = 0
SecondNumber = 0
ResultText.Text = "0"
Symbol.Text = ""
end,
}
function AddCommas(Number)
Number = tostring(Number)
local Split = string.split(Number,"")
if #Split > 3 then
for i = #Split,1,-3 do
Split[i] = i ~= #Split and Split[i].."," or Split[i]
end
end
Number = ""
for i = 1,#Split,1 do Number = Number..Split[i] end
return Number
end
for i,Button in pairs(ButtonsHolder:GetChildren()) do
if Button:IsA("TextButton") then
Button.MouseButton1Down:Connect(function()
if tonumber(Button.Text) or Button.Text == "." then
if SwitchToSecondNumber then
SecondNumber = (SecondNumber == 0 and Button.Text ~=".") and Button.Text or tostring(SecondNumber)..Button.Text
ResultText.Text = AddCommas(SecondNumber)
else
FirstNumber = (FirstNumber == 0 and Button.Text ~=".") and Button.Text or tostring(FirstNumber)..Button.Text
ResultText.Text = AddCommas(FirstNumber)
end
elseif MathFunctions[Button.Text] then
Symbol.Text = Button.Text
SwitchToSecondNumber = true
SymbolSelected = Button.Text
elseif Symbols[string.upper(Button.Text)] then Symbols[string.upper(Button.Text)]() end
end)
end
end
Ended up using my own script after messing with it for a bit! I used a little bit of everyone’s idea’s and ended up with this, if anyone needs to use it!
--// Items
--# Objects
local Calculator = script.Parent
local One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Zero = Calculator:FindFirstChild('One'), Calculator:FindFirstChild('Two'), Calculator:FindFirstChild('Three'), Calculator:FindFirstChild('Four'), Calculator:FindFirstChild('Five'), Calculator:FindFirstChild('Six'), Calculator:FindFirstChild('Seven'), Calculator:FindFirstChild('Eight'), Calculator:FindFirstChild('Nine'), Calculator:FindFirstChild('Zero')
local Equal, Add, Subtract, Multiply, Divide = Calculator:FindFirstChild('Equal'), Calculator:FindFirstChild('Add'), Calculator:FindFirstChild('Subtract'), Calculator:FindFirstChild('Multiply'), Calculator:FindFirstChild('Divide')
local Clear, System = Calculator:FindFirstChild('Clear'), Calculator:FindFirstChild('System')
local Output = Calculator:FindFirstChild('Output')
--# Variables
local valueOne = 0
local valueTwo = 0
local Operation = nil
local Solution = nil
--// Scripts
--# numberClicked
local function numberClicked(numberClicked)
if Solution then
valueOne, valueTwo = 0, 0
Operation, Solution = nil, nil
end
if Operation == nil then
if valueOne == 0 then
valueOne = numberClicked
elseif valueOne == '-0' then
valueOne = '-'..numberClicked
else
valueOne = valueOne..numberClicked
end
else
if valueTwo == 0 then
valueTwo = numberClicked
elseif valueTwo == '-0' then
valueTwo = '-'..numberClicked
else
valueTwo = valueTwo..numberClicked
end
end
end
One.Activated:Connect(function() numberClicked(1) end)
Two.Activated:Connect(function() numberClicked(2) end)
Three.Activated:Connect(function() numberClicked(3) end)
Four.Activated:Connect(function() numberClicked(4) end)
Five.Activated:Connect(function() numberClicked(5) end)
Six.Activated:Connect(function() numberClicked(6) end)
Seven.Activated:Connect(function() numberClicked(7) end)
Eight.Activated:Connect(function() numberClicked(8) end)
Nine.Activated:Connect(function() numberClicked(9) end)
Zero.Activated:Connect(function() numberClicked(0) end)
--# operationClicked
local function operationClicked(operationClicked)
if operationClicked == '+' then
Operation = '+'
elseif operationClicked == '-' then
Operation = '-'
elseif operationClicked == '*' then
Operation = '*'
elseif operationClicked == '/' then
Operation = '/'
elseif operationClicked == '=' then
if (valueOne ~= 0) and (valueTwo ~= 0) then
valueOne = tonumber(valueOne)
valueTwo = tonumber(valueTwo)
if Operation == '+' then
Solution = (valueOne + valueTwo)
elseif Operation == '-' then
Solution = (valueOne - valueTwo)
elseif Operation == '*' then
Solution = (valueOne * valueTwo)
elseif Operation == '/' then
Solution = (valueOne / valueTwo)
end
end
end
end
Add.Activated:Connect(function() operationClicked('+') end)
Subtract.Activated:Connect(function() operationClicked('-') end)
Multiply.Activated:Connect(function() operationClicked('*') end)
Divide.Activated:Connect(function() operationClicked('/') end)
Equal.Activated:Connect(function() operationClicked('=') end)
--# buttonClicked
local function buttonClicked(buttonClicked)
if buttonClicked == 'clear' then
valueOne, valueTwo = 0, 0
Operation, Solution = nil, nil
elseif buttonClicked == 'sys' then
if (Solution) then return end
if Operation then
valueTwo = '-'..valueTwo
else
valueOne = '-'..valueOne
end
end
end
Clear.Activated:Connect(function() buttonClicked('clear') end)
System.Activated:Connect(function() buttonClicked('sys') end)
--# Show Text
while true do
if (not valueOne) and (not valueTwo) then
Output.Text = '0'
elseif (valueOne) and (not Operation) then
Output.Text = valueOne
elseif (valueTwo) and (not Solution) then
Output.Text = valueTwo
elseif (Solution) then
Output.Text = Solution
end
wait()
end
is there a way to use more than two values but still have a limit?
I haven’t worked on the project for a while but, here is the final script I ended up with:
--// Items
--# Objects
local Calculator = script.Parent
local One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Zero = Calculator:FindFirstChild('One'), Calculator:FindFirstChild('Two'), Calculator:FindFirstChild('Three'), Calculator:FindFirstChild('Four'), Calculator:FindFirstChild('Five'), Calculator:FindFirstChild('Six'), Calculator:FindFirstChild('Seven'), Calculator:FindFirstChild('Eight'), Calculator:FindFirstChild('Nine'), Calculator:FindFirstChild('Zero')
local Equal, Add, Subtract, Multiply, Divide = Calculator:FindFirstChild('Equal'), Calculator:FindFirstChild('Add'), Calculator:FindFirstChild('Subtract'), Calculator:FindFirstChild('Multiply'), Calculator:FindFirstChild('Divide')
local Clear, System = Calculator:FindFirstChild('Clear'), Calculator:FindFirstChild('System')
local Output = Calculator:FindFirstChild('Output')
--# Variables
local valueOne = 0
local valueTwo = 0
local Operation = nil
local Solution = nil
--// Scripts
--# numberClicked
local function numberClicked(numberClicked)
if Operation == nil then
if valueOne == 0 then
valueOne = numberClicked
elseif valueOne == '-0' then
valueOne = '-'..numberClicked
else
valueOne = valueOne..numberClicked
end
else
if valueTwo == 0 then
valueTwo = numberClicked
elseif valueTwo == '-0' then
valueTwo = '-'..numberClicked
else
valueTwo = valueTwo..numberClicked
end
end
end
One.Activated:Connect(function() numberClicked(1) end)
Two.Activated:Connect(function() numberClicked(2) end)
Three.Activated:Connect(function() numberClicked(3) end)
Four.Activated:Connect(function() numberClicked(4) end)
Five.Activated:Connect(function() numberClicked(5) end)
Six.Activated:Connect(function() numberClicked(6) end)
Seven.Activated:Connect(function() numberClicked(7) end)
Eight.Activated:Connect(function() numberClicked(8) end)
Nine.Activated:Connect(function() numberClicked(9) end)
Zero.Activated:Connect(function() numberClicked(0) end)
--# operationClicked
local function operationClicked(operationClicked)
if (Solution) then
valueOne, valueTwo = Solution, 0
Solution = nil
end
if operationClicked == '+' then
Operation = '+'
elseif operationClicked == '-' then
Operation = '-'
elseif operationClicked == '*' then
Operation = '*'
elseif operationClicked == '/' then
Operation = '/'
elseif operationClicked == '=' then
if (valueOne ~= 0) and (valueTwo ~= 0) then
valueOne = tonumber(valueOne)
valueTwo = tonumber(valueTwo)
if Operation == '+' then
Solution = (valueOne + valueTwo)
elseif Operation == '-' then
Solution = (valueOne - valueTwo)
elseif Operation == '*' then
Solution = (valueOne * valueTwo)
elseif Operation == '/' then
Solution = (valueOne / valueTwo)
end
end
end
end
Add.Activated:Connect(function() operationClicked('+') end)
Subtract.Activated:Connect(function() operationClicked('-') end)
Multiply.Activated:Connect(function() operationClicked('*') end)
Divide.Activated:Connect(function() operationClicked('/') end)
Equal.Activated:Connect(function() operationClicked('=') end)
--# buttonClicked
local function buttonClicked(buttonClicked)
if buttonClicked == 'clear' then
valueOne, valueTwo = 0, 0
Operation, Solution = nil, nil
elseif buttonClicked == 'sys' then
if (Solution) then return end
if Operation then
valueTwo = '-'..valueTwo
else
valueOne = '-'..valueOne
end
end
end
Clear.Activated:Connect(function() buttonClicked('clear') end)
System.Activated:Connect(function() buttonClicked('sys') end)
--# Show Text
while true do
if (not valueOne) and (not valueTwo) then
Output.Text = '0'
elseif (valueOne) and (not Operation) then
Output.Text = valueOne
elseif (valueTwo) and (not Solution) then
Output.Text = valueTwo
elseif (Solution) then
Output.Text = Solution
end
wait()
end