Code rarely does not work with no errors given

I have the following code, it’s for a keypad door, it works almost all the time however rarely some of the buttons that register key inputs don’t work and as such it does not work. No errors are printed when this happens and I don’t know why it does this

code:

local referance = game.Workspace:WaitForChild("Important"):WaitForChild("SecurityRoomDoor"):WaitForChild('Code'):WaitForChild("Thing")

local db = false

local doorStatus = "Closed"

local screen = referance.Parent:WaitForChild("ScreenPart"):WaitForChild("SurfaceGui"):WaitForChild("TextLabel")
local confirm = referance.Parent:WaitForChild("Confirm")
local ProximityPrompt = referance.Parent:WaitForChild("PromptPart"):WaitForChild("CodePrompt")
local cameraPart = referance.Parent:WaitForChild("CameraPart")
local main = referance.Parent.Parent:WaitForChild("Main")
local insideButton = referance.Parent:WaitForChild("InsideButton")
local UIButton = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("ExitCode"):WaitForChild("TextButton")
local camera

local oldCF

local TS = game:GetService("TweenService")
local info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)

local closePos = {51.526, 4.847, 18.676}
local openPos = {51.526, 11.959, 18.676}

local code = 9124

ProximityPrompt.Triggered:Connect(function()
	if db then return end
	db = true
	UIButton.Visible = true
	ProximityPrompt.Enabled = false
	camera = game.Workspace.CurrentCamera
	oldCF = camera.CFrame
	camera.CameraType = Enum.CameraType.Scriptable
	local tween = TS:Create(camera, info, {CFrame = cameraPart.CFrame})
	tween:Play()
	task.wait(1)
	db = false
end)

UIButton.MouseButton1Click:Connect(function()
	if db then return end
	db = true
	screen.Text = ""
	camera = game.Workspace.CurrentCamera
	camera.CameraType = Enum.CameraType.Scriptable
	local tween = TS:Create(camera, info, {CFrame = oldCF})
	UIButton.Visible = false
	ProximityPrompt.Enabled = true
	tween:Play()
	task.wait(1)
	camera.CameraType = Enum.CameraType.Custom
	task.wait(1)
	db = false
end)


for i, v in pairs(referance.Parent.Buttons:GetChildren()) do
	v.ClickDetector.MouseClick:Connect(function()
		if doorStatus == "Closed" and db == false and #screen.Text < 4 then
			screen.Text = screen.Text .. v.Name
		end
	end)
end

function flashWrongCode()

	screen.TextColor3 = Color3.new(1, 0, 0.0156863)  
	for i = 1, 5 do
		screen.TextTransparency = 0
		task.wait(0.2) 
		screen.TextTransparency = 1
		task.wait(0.2)
	end
	screen.TextTransparency = 0
	screen.TextColor3 = Color3.new(1, 1, 1)  
	screen.Text = ""

end


confirm.ClickDetector.MouseClick:Connect(function()
	if db or doorStatus == "Open" then return end
	db = true
	if tonumber(screen.Text) == code then
		doorStatus = "Open"
		screen.TextColor3 = Color3.new(0, 1, 0.0156863)  
		UIButton.Visible = false
		game.SoundService["Game Sound Correct"]:Play()
		for i = 1,100 do
			main.Position += Vector3.new(0, 0.07112, 0)
			task.wait(0.001)
		end
		camera = game.Workspace.CurrentCamera
		local tween = TS:Create(camera, info, {CFrame = oldCF})
		tween:Play()
		task.wait(1)
		screen.TextColor3 = Color3.new(1, 1, 1) 
		screen.Text = ""
		camera.CameraType = Enum.CameraType.Custom
		ProximityPrompt.Enabled = true
		db = false
	else
	--	screen.Text = ""
		game.SoundService["Menu Error"]:Play()
		flashWrongCode()
		task.wait(1)
		db = false
	end
end)

insideButton.ClickDetector.MouseClick:Connect(function()
	if db then return end
	if doorStatus == "Open" then
		db = true	
		doorStatus = "Closed"
		insideButton.BrickColor = BrickColor.new("Sea green")
		for i = 1,100 do
			main.Position -= Vector3.new(0, 0.07112, 0)
			task.wait(0.001)
		end
		task.wait(1)
		db = false
	else
		db = true
		doorStatus = "Open"
		insideButton.BrickColor = BrickColor.new("Really red")
		for i = 1,100 do
			main.Position += Vector3.new(0, 0.07112, 0)
			task.wait(0.001)
		end
		task.wait(1)
		db = false
	end
end)
1 Like

is it possible for you to record a video of the problem happening? i think it would help others and i to understand/solve the problem.

1 Like

Problem is I play the game like 20 times to test the code door. And it works 19 of the 20 times. As stated there are no errors and the bug is in that some of the keys from 1-9 which have the click detectors in them don’t register clicks. So you click on a key and only in the rare case that it breaks it doesn’t do the logic for handing the click.

When you click on a button then it doesn’t work does it stay broken or starts working the next time you click.

If it breaks it will remain broken until I leave the game and re run it

You said you play like 20 times and maybe 1 time it doesn’t work.

Does it stop working during gameplay or does it not work at all on the 1 time it breaks?

Only during gameplay. If I leave and re run the game it will start working again. I didn’t even know the bug existed for a while because it just worked up until it didn’t for the first time

If it breaks randomly during gameplay then I am going to guess “db” has something to do with it.

I added a print command to your script to check the status of “db”.

Maybe try it out and see if you can see a moment where db is wrong:

local referance = game.Workspace:WaitForChild("Important"):WaitForChild("SecurityRoomDoor"):WaitForChild('Code'):WaitForChild("Thing")

local db = false

local doorStatus = "Closed"

local screen = referance.Parent:WaitForChild("ScreenPart"):WaitForChild("SurfaceGui"):WaitForChild("TextLabel")
local confirm = referance.Parent:WaitForChild("Confirm")
local ProximityPrompt = referance.Parent:WaitForChild("PromptPart"):WaitForChild("CodePrompt")
local cameraPart = referance.Parent:WaitForChild("CameraPart")
local main = referance.Parent.Parent:WaitForChild("Main")
local insideButton = referance.Parent:WaitForChild("InsideButton")
local UIButton = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("ExitCode"):WaitForChild("TextButton")
local camera
local debug = true

local oldCF

local TS = game:GetService("TweenService")
local info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)

local closePos = {51.526, 4.847, 18.676}
local openPos = {51.526, 11.959, 18.676}

local code = 9124

ProximityPrompt.Triggered:Connect(function()
	if db then if debug then print("db return") end return end
	db = true
	UIButton.Visible = true
	ProximityPrompt.Enabled = false
	camera = game.Workspace.CurrentCamera
	oldCF = camera.CFrame
	camera.CameraType = Enum.CameraType.Scriptable
	local tween = TS:Create(camera, info, {CFrame = cameraPart.CFrame})
	tween:Play()
	task.wait(1)
	db = false
end)

UIButton.MouseButton1Click:Connect(function()
	if db then if debug then print("db return") end return end
	db = true
	screen.Text = ""
	camera = game.Workspace.CurrentCamera
	camera.CameraType = Enum.CameraType.Scriptable
	local tween = TS:Create(camera, info, {CFrame = oldCF})
	UIButton.Visible = false
	ProximityPrompt.Enabled = true
	tween:Play()
	task.wait(1)
	camera.CameraType = Enum.CameraType.Custom
	task.wait(1)
	db = false
end)


for i, v in pairs(referance.Parent.Buttons:GetChildren()) do
	v.ClickDetector.MouseClick:Connect(function()
		if doorStatus == "Closed" and db == false and #screen.Text < 4 then
			screen.Text = screen.Text .. v.Name
		end
	end)
end

function flashWrongCode()

	screen.TextColor3 = Color3.new(1, 0, 0.0156863)  
	for i = 1, 5 do
		screen.TextTransparency = 0
		task.wait(0.2) 
		screen.TextTransparency = 1
		task.wait(0.2)
	end
	screen.TextTransparency = 0
	screen.TextColor3 = Color3.new(1, 1, 1)  
	screen.Text = ""

end


confirm.ClickDetector.MouseClick:Connect(function()
	if db or doorStatus == "Open" then if debug then print("db or Open return") end return end
	db = true
	if tonumber(screen.Text) == code then
		doorStatus = "Open"
		screen.TextColor3 = Color3.new(0, 1, 0.0156863)  
		UIButton.Visible = false
		game.SoundService["Game Sound Correct"]:Play()
		for i = 1,100 do
			main.Position += Vector3.new(0, 0.07112, 0)
			task.wait(0.001)
		end
		camera = game.Workspace.CurrentCamera
		local tween = TS:Create(camera, info, {CFrame = oldCF})
		tween:Play()
		task.wait(1)
		screen.TextColor3 = Color3.new(1, 1, 1) 
		screen.Text = ""
		camera.CameraType = Enum.CameraType.Custom
		ProximityPrompt.Enabled = true
		db = false
	else
		--	screen.Text = ""
		game.SoundService["Menu Error"]:Play()
		flashWrongCode()
		task.wait(1)
		db = false
	end
end)

insideButton.ClickDetector.MouseClick:Connect(function()
	if db then if debug then print("db return") end return end
	if doorStatus == "Open" then
		db = true	
		doorStatus = "Closed"
		insideButton.BrickColor = BrickColor.new("Sea green")
		for i = 1,100 do
			main.Position -= Vector3.new(0, 0.07112, 0)
			task.wait(0.001)
		end
		task.wait(1)
		db = false
	else
		db = true
		doorStatus = "Open"
		insideButton.BrickColor = BrickColor.new("Really red")
		for i = 1,100 do
			main.Position += Vector3.new(0, 0.07112, 0)
			task.wait(0.001)
		end
		task.wait(1)
		db = false
	end
end)

If it is breaking on load then I am going to guess one of the stacked :WaitForChild variables never actually loaded and is nil; causing a failure.