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:
Here is what the keypad looks like:
The numbers and everything is good just it repeats the printing Wayyyy to much.
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!
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
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
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