Code is repeating too many times!

So I am trying to make a security passcode kind of thing, but whenever I click a button on the keypad the game lags and prints that key over 26 times. Here is the code:

local CodeNum = script.Parent.CodeNum
local Ecode = script.Parent.EnterCode
local Buttons = script.Parent.Buttons

local ButtonChildren = Buttons:GetChildren()

local Code = ""
script.Parent.EnterCode.Text = Code

while wait(.2) do
	
	for i = 1,#ButtonChildren do
		
		ButtonChildren[i].MouseButton1Click:Connect(function()
			
			local Name = ButtonChildren[i].Name
			
			local ButtonNum = string.sub(Name,2,2)
			
			print(ButtonNum)
			
			Code = Code..ButtonNum
			
			script.Parent.EnterCode.Text = Code
			
		end)
		
	end
	
end

Explorer:

image

Here is what the keypad looks like:

image

The numbers and everything is good just it repeats the printing Wayyyy to much.

Is there a reason you have a while loop? That is the problem, since it creates 5 connections a second.

the while loop is because I am too lazy to add a script into EVERY single button, so I added a while loop so that a script can just repeat forever and it checks if any buttons got pressed using the for loop!

Wouldn’t you just remove the loop to prevent it from repeating?

Add a wait(.12) before:

ButtonChildren[i].MouseButton1Click:Connect(function()
1 Like

No, literally remove the while loop.

Take the code out of the while loop.

for i = 1,#ButtonChildren do
	ButtonChildren[i].MouseButton1Click:Connect(function()
			
		local Name = ButtonChildren[i].Name
		local ButtonNum = string.sub(Name,2,2)
		print(ButtonNum)
		Code = Code..ButtonNum	
		script.Parent.EnterCode.Text = Code
			
	end)
end
2 Likes
local CodeNum = script.Parent.CodeNum
local Ecode = script.Parent.EnterCode
local Buttons = script.Parent.Buttons


script.Parent.EnterCode.Text = ""
for i,v in pairs(Buttons:GetChildren()) do
	v.MouseButton1Click:Connect(function()
		local ButtonNum = v.Name:sub(2,2)
		print(ButtonNum)
		script.Parent.EnterCode.Text = script.Parent.EnterCode.Text..ButtonNum
	end)
end
1 Like

I need to keep the loop, because I need to check every single button in the frame that is named buttons.

Look here are all the buttons:

image

no you don’t. The button press is the event.

Just try it.

You can just iterate through every children.

OOOH i get it now sry, it works now that I got rid of the while loop, thx!

Because you’re using a while loop and a for loop.

You could’ve just use the debounce method. For example[quote=“creepersaur, post:1, topic:810102”]

while wait(.2) do
	
	for i = 1,#ButtonChildren do
		
		ButtonChildren[i].MouseButton1Click:Connect(function()
			
			local Name = ButtonChildren[i].Name
			
			local ButtonNum = string.sub(Name,2,2)
			
			print(ButtonNum)
			
			Code = Code..ButtonNum
			
			script.Parent.EnterCode.Text = Code
			
		end)
		
	end
	
end

[/quote]

This is the fix.

local db= false
while true do
    for i = 1,#ButtonChildren do
        ButtonChildren[1].MouseButton1Click:Connect(function()
            db = true
            — do ur code here.
            wait(1) — after u click a button, wait for a sec
            db = false
        end)
    end
end