Hey guys I’m making a calculator. There is an issue though. It’s just so tedious to write! Is there a way to make this better? (This is for addition)
local addition = function(num1,num2)
if num1 == 1 and num2 == 0 then
return 1
else if num1 == 1 and num2 == 1 then
return 2
else if num1 == 1 and num2 == 2 then
return 3
else if num1 == 1 and num2 == 3 then
return 4
else if num1 == 1 and num2 == 4 then
return 5
else if num1 == 1 and num2 == 5 then
return 6
else if num1 == 1 and num2 == 6 then
return 7
else if num1 == 1 and num2 == 7 then
return 8
else if num1 == 1 and num2 == 8 then
return 9
else if num1 == 1 and num2 == 9 then
return 10
else if num1 == 1 and num2 == 10 then
return 11
end
end
-- Add the two numbers provided (NOTE: the ": number" is a type annotation; it's like saying "This IS a number". It is not required)
local addition = function(num1: number, num2: number)
return num1 + num2
end
Num1 and Num2 are simply the names of these arguments; what you provide to the function for it to work with.
For example:
local function addition(num1: number, num2: number)
return num1 + num2
end
-- Num1 would be equal to 1, and num2 would be equal to 3.
addition(1, 3) -- Returns 4
So, if you are trying to count the rocks in your game, you could do the following:
Put all the rocks into a folder in the workspace, we’ll call it “Rocks” for now.
Write an 'ol function that counts the number of rocks in that folder… so:
local Rocks = workspace.Rocks
-- Returns the amount of rocks
local function countRocks()
return #Rocks:GetChildren() -- # is the number of something
end
-- These basically fire each time a rock is added or removed.
Rocks.DescendantAdded:Connect(countRocks)
Rocks.DescendantRemoving:Connect(countRocks)
Sorry if I misunderstood.
Oh – and don’t forget to put the rock inside that folder wherever you make your rocks
Ah – that’s my game! Let me explain what’s happening:
The folder is inside the workspace for the rocks. Since the folder is in the workspace, anything inside said folder will be visible in the workspace. I hope I explained that correctly.