local Phone = script.Parent
local NumberOne = 0
local NumberTwo = 0
local NumberThree = 0
Phone.One.ClickDetector.MouseClick:Connect(function(hit)
if NumberOne == 0 then
NumberOne = 1
else
if NumberTwo == 0 then
NumberTwo = 1
else
if NumberThree == 0 then
NumberThree = 1
else
NumberThree = 1
end
end
end
end)
Phone.Two.ClickDetector.MouseClick:Connect(function(hit)
if NumberOne == 0 then
NumberOne = 2
else
if NumberTwo == 0 then
NumberTwo = 2
else
if NumberThree == 0 then
NumberThree = 2
else
NumberThree = 2
end
end
end
end)
Phone.Three.ClickDetector.MouseClick:Connect(function(hit)
if NumberOne == 0 then
NumberOne = 3
else
if NumberTwo == 0 then
NumberTwo = 3
else
if NumberThree == 0 then
NumberThree = 3
else
NumberThree = 3
end
end
end
end)
Phone.Four.ClickDetector.MouseClick:Connect(function(hit)
if NumberOne == 0 then
NumberOne = 4
else
if NumberTwo == 0 then
NumberTwo = 4
else
if NumberThree == 0 then
NumberThree = 4
else
NumberThree = 4
end
end
end
end)
Phone.Five.ClickDetector.MouseClick:Connect(function(hit)
if NumberOne == 0 then
NumberOne = 5
else
if NumberTwo == 0 then
NumberTwo = 5
else
if NumberThree == 0 then
NumberThree = 5
else
NumberThree = 5
end
end
end
end)
Phone.Six.ClickDetector.MouseClick:Connect(function(hit)
if NumberOne == 0 then
NumberOne = 6
else
if NumberTwo == 0 then
NumberTwo = 6
else
if NumberThree == 0 then
NumberThree = 6
else
NumberThree = 6
end
end
end
end)
Phone.Seven.ClickDetector.MouseClick:Connect(function(hit)
if NumberOne == 0 then
NumberOne = 7
else
if NumberTwo == 0 then
NumberTwo = 7
else
if NumberThree == 0 then
NumberThree = 7
else
NumberThree = 7
end
end
end
end)
Phone.Eight.ClickDetector.MouseClick:Connect(function(hit)
if NumberOne == 0 then
NumberOne = 8
else
if NumberTwo == 0 then
NumberTwo = 8
else
if NumberThree == 0 then
NumberThree = 8
else
NumberThree = 8
end
end
end
end)
Phone.Nine.ClickDetector.MouseClick:Connect(function(hit)
if NumberOne == 0 then
NumberOne = 9
else
if NumberTwo == 0 then
NumberTwo = 9
else
if NumberThree == 0 then
NumberThree = 9
else
NumberThree = 9
end
end
end
end)
Phone.EnterButton.ClickDetector.MouseClick:Connect(function(hit)
print(NumberOne,NumberTwo,NumberThree)
end)
Phone.DeleteButton.ClickDetector.MouseClick:Connect(function(hit)
if NumberThree > 0 then
NumberThree = 0
else
if NumberTwo > 0 then
NumberTwo = 0
else
if NumberOne > 0 then
NumberThree = 0
end
end
end
end)
Phone.ClearButton.ClickDetector.MouseClick:Connect(function(hit)
NumberOne = 0
NumberTwo = 0
NumberThree = 0
end)
yeah as you can see, this code is a bit long. Its mostly just the same function repeated but with different buttons. Im wondering if there is any way to possibly shorten this?
also EDIT: if there IS a way to shorten this, is it still better to keep it like this?
It will print “3”.
So basically, you can use this in a script to avoid lots of if statements.
So I’ll do the first part for you:
Phone.One.ClickDetector.MouseClick:Connect(function(hit)
NumberOne = (NumberOne == 0 and 1)
NumberTwo = ((NumberOne ~= 0 and NumberTwo == 0) and 1 or 0)
NumberThree = ((NumberOne ~= 0 and NumberThree == 0) and 1 or 0)
end)
the thing about this script is that i dont want them to all go at once
so like (sorry for not being specfic) i made it so you can click 3 different buttons and depending on the buttons you click, the output will consist of them and the order you did it.
Using your script
If i click 1, 1, 1, the output is
However, if i clear and click it once, the output is
(i might be wrong im not really good at coding)
local Phone = script.Parent
for Index, Child in ipairs(Phone:GetChildren()) do
local CD = Child:FindFirstChild("ClickDetector")
if CD then
CD.MouseClick:Connect(function(Player)
print(Player.Name) --add whatever each clickdetector should do here when clicked
end)
end
end
This is a horrid misuse of if statements. If you want to associate certain values with other values you should use a dictionary.
Also, I’m not quite sure what you are making, but it appears you are entering numbers to get a phone number? If that’s the case, this can be done much more efficiently using a string instead.
local Phone = script.Parent
local PhoneNumber = ""
local values = {
One = "1",
Two = "2",
Three = "3",
Four = "4",
Five = "5",
Six = "6",
Seven = "7",
Eight = "8",
Nine = "9",
Zero = "0",
EnterButton = function() print(PhoneNumber) end,
DeleteButton = function() PhoneNumber = PhoneNumber:sub(1, PhoneNumber:len() - 1) end,
ClearButton = function() PhoneNumber = "" end
}
for _, child in pairs(Phone:GetChildren()) do
local value = values[child.Name]
if value then
if typeof(value) == "string" then
child.ClickDetector.MouseClick:Connect(function() PhoneNumber ..= value end)
else
value()
end
end
end
Alternatively, instead of using a table you could name the parts the actual number instead of the word for the number.
local Phone = script.Parent
local Clicks = {}
for Index, Child in ipairs(Phone:GetChildren()) do
local CD = Child:FindFirstChild("ClickDetector")
if CD then
CD.MouseClick:Connect(function(Player)
Clicks[Index] = {Index, tick()}
if #Clicks == 3 then
table.sort(Clicks, function(Left, Right)
if Left[2] < Right[2] then
return Right
end
end)
for _, Value in ipairs(Clicks) do
print(Value[1])
end
Clicks = {}
end
end)
end
end
After seeing what you’re trying to achieve, here.
I tested this in studio and it works.
It records the time at which each ClickDetector is clicked and when all 3 have been clicked prints the order that the ClickDetectors were clicked in.
On the phone, there Are 9 numbers
1 2 3
4 5 6
7 8 9
Bk enter
Each of these numbers are a different part.
Now let’s say someone clicks the numbers 4 8 8
If they press enter, 488 should print out.
That’s basically all it needs to do.
(You can only click 3 numbers by the way)
Im sorry if I’m bothering anyone with the posts I’ve been making about easy/unclarified topics. I plan to make posts about questions less frequently and more reasonable after this one. I’m Also super happy that there is a community like this and it’s one of my most favorites by far