Loop dont work [CODE PORT]

I’m making a port that needs a code to open, but some of the numbers don’t work

These are the keys that don’t work:

image

The part of my code that picks up the keys is this:

The Keyboard variable is a model that is all key:

image

Would anyone have any idea why these keys don’t work?

I think this should be a different way of doing the same thing. Maybe looking at it a different way gives some clues.

The ‘>’ doesn’t have an action assigned to it.
The rest should work unless I missed a typo. I changed the for loop from the for i,v in next version you had to ipairs(). Maybe there’s some weirdness with that.

--[[
- Pressing '>' does nothing ever
- Pressing 'X' while text is "Enter code" does nothing
- Pressing 'X' while text is <not "Enter code"> replaces text with "Enter code"
- Pressing # while text is "Enter code" replaces text with the #
- Pressing # while text is <not "Enter code"> appends the # to the end of text
]]
for _,v in ipairs(KeyBoard:GetDescendants()) do
	if v:IsA("TextButton") then
		
		v.MouseButton1Down:Connect(function() 
			if not Debounce then
				Debounce = true
				if ScreenTextLabel.Text == "Enter code" then
					if v.Text ~= ">" and v.Text ~= "X" then
						-- if Text is "Enter code" and anything except '>' or 'X'
						ScreenTextLabel.Text = ""
						ScreenTextLabel.Text = ScreenTextLabel.Text..v.Text
						ClickSound:Play()
					else
						-- if Text is "Enter code" and either '>' or 'X' is pressed
						-- do nothing
					end
				else	
					-- if Text is not "Enter code"
					if v.Text == "X" then
						ScreenTextLabel.Text = "Enter code"
					elseif v.Text == ">" then
						-- not set
					else
						-- Text is not "Enter code" + anything that isn't '>' or 'X'
						ScreenTextLabel.Text = ScreenTextLabel.Text..v.Text
						ClickSound:Play()
					end
				end	
				wait(1)
				Debounce = false
			end
		end)
		
	end
end

This does not solve your problem, but I suggest replacing all wait()s with task.wait(), as that would make the flow smoother and task.wait works on heartbeats, whilst wait works on whatever garbage that is slower.

And wait() will soon be deprecated in favor of task.wait() so you might aswell make the change now.

I tried cleaning your code if statements so it’s much more readable. Also there’s a slight chance the issue fixed itself:

local default = "Enter code" --default text

for _, key in pairs(Keyboard:GetDescendants()) do 
	if not key:IsA("TextButton") then continue end --not a button, ignore
	key.MouseButton1Down:Connect(function()
		if Debounce then return end --debounce is active, ignore
		Debounce = true 
		ClickSound:Play() 
		if ScreenTextLabel.Text == default then 
			ScreenTextLabel.Text = "" 
		end
		if key.Text == ">" then 
			print("the code is "..ScreenTextLabel.Text)
			task.wait(2) 
			ScreenTextLabel.Text = default 
		elseif key.Text == "X" then 
			ScreenTextLabel.Text = default 
		elseif tonumber(key.Text) then 
			ScreenTextLabel.Text ..= key.Text 
		end
		task.wait(1)
		Debounce = false 
	end)
end

You likely have overlapping buttons and/or misconfigured buttons. It is also possible that the “ZIndex” properties of your GuiObject instances are causing issues.