Any way to shorten this?

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?

Turning all the this:

if
    —code
else
    if
        —code
    else
        if
            —code
        end
    end
end

into this:

if
    —code
elseif 
    —code
elseif
    —code
end

could majorly shorten that code.

Hiya,

If you go to studio and in a script type

print(2+2 == 4 and 3)

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
image
However, if i clear and click it once, the output is
image
(i might be wrong im not really good at coding)

If you just want only one to change at a time, add a debounce

e.g. after the value is changed, it goes to true, then at end of code goes back to false

Remember if you want code suggestions / improvements, you put the post in #help-and-feedback:code-review, not scripting support.

1 Like
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.

Would be easier to use the new ternary operators from October Luau Recap, they support elseif

a = 5
b = 6
c = if a == 10 then 2 elseif b == 6 then 4 else 2

print(c)
-- : 4

This is my bad. I didn’t explain too well

Let’s say there is a phone:

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

local NumberOne, NumberTwo, NumberThree = 0

This is a good practice when you’re assigning many variables the same value.

local Phone = script.Parent
local NumbersEntered = {},
local NumberButtons = {
    ["One"] = 1,
    ["Two"] = 2,
    ["Three"] = 3,
    ["Four"] = 4,
    ["Five"] = 5,
    ["Six"] = 6,
    ["Seven"] = 7,
    ["Eight"] = 8,
    ["Nine"] = 9
    --Why doesn't your phone have a zero key?
}

for index, object in ipairs(Phone:GetChildren()) do
    if NumberButtons[object.Name] ~= nil then
        object.ClickDetector.MouseClick:Connect(function()
            table.insert(NumbersEntered, NumberButtons[object.Name])
        end)
    end
end

Phone.EnterButton.ClickDetector.MouseClick:Connect(function()  
	print(table.concat(NumbersEntered, ", "))
end)

Phone.DeleteButton.ClickDetector.MouseClick:Connect(function()
	NumbersEntered[#NumbersEntered] = nil
end)

Phone.ClearButton.ClickDetector.MouseClick:Connect(function()
	table.clear(NumbersEntered)
end)

Thanks to tables, the code is now very short while maintaining the functionality of the original, and you can add more than 3 numbers now.

If there’s any errors, let me know!

Replied to the wrong person, was meant for OP.

That will set NumberOne to 0 but NumberTwo and NumberThree to nil, which is not what is intended :slight_smile:

e.g. test in command bar

local a,b,c = 0; print(a,b,c) -- 0 nil nil

a, b, and c would all need to be set to 0 by doing this:

local a,b,c = 0,0,0; print(a,b,c):

Correct.

Post must be at least 30.