Weird if statement glitch happening (fixed)

Hi everyone, I made a working CodeTab. It generates a code with math.random and when I type in the code I can clear everything I typed in or accept it. The accept button has an if statement where it checks if the input is the same as the correctcode. Somehow when I type in the code it always shows Wrong Code! No matter what I change in the script it always shows the Wrong Code! Heres an example:

local Keys = script.Parent.Keys
local Screen = script.Parent.Screen

local CorrectCode = math.random(100000,999999)
print("Code:"..CorrectCode)
local input = ""

local function Key0()
	input = input.."0"
	Screen.SurfaceGui.TextLabel.Text = input
	
	print(input)
end

Keys.Key0.ClickDetector.MouseClick:Connect(Key0)

local function Key1()
	input = input.."1"
	Screen.SurfaceGui.TextLabel.Text = input

	print(input)
end

Keys.Key1.ClickDetector.MouseClick:Connect(Key1)

local function Key2()
	input = input.."2"
	Screen.SurfaceGui.TextLabel.Text = input

	print(input)
end

Keys.Key2.ClickDetector.MouseClick:Connect(Key2)

local function Key3()
	input = input.."3"
	Screen.SurfaceGui.TextLabel.Text = input

	print(input)
end

Keys.Key3.ClickDetector.MouseClick:Connect(Key3)

local function Key4()
	input = input.."4"
	Screen.SurfaceGui.TextLabel.Text = input

	print(input)
end

Keys.Key4.ClickDetector.MouseClick:Connect(Key4)

local function Key5()
	input = input.."5"
	Screen.SurfaceGui.TextLabel.Text = input

	print(input)
end

Keys.Key5.ClickDetector.MouseClick:Connect(Key5)

local function Key6()
	input = input.."6"
	Screen.SurfaceGui.TextLabel.Text = input

	print(input)
end

Keys.Key6.ClickDetector.MouseClick:Connect(Key6)

local function Key7()
	input = input.."7"
	Screen.SurfaceGui.TextLabel.Text = input

	print(input)
end

Keys.Key7.ClickDetector.MouseClick:Connect(Key7)

local function Key8()
	input = input.."8"
	Screen.SurfaceGui.TextLabel.Text = input

	print(input)
end

Keys.Key8.ClickDetector.MouseClick:Connect(Key8)

local function Key9()
	input = input.."9"
	Screen.SurfaceGui.TextLabel.Text = input

	print(input)
end

Keys.Key9.ClickDetector.MouseClick:Connect(Key9)

local function Accept()
	print(input)
	if input == CorrectCode then
		print("ACCES GRANTED!")

	else	print("ERROR! WRONG CODE!")
		input = ""
		
	end
end

script.Parent.Accept.ClickDetector.MouseClick:Connect(Accept)

local function Decline()
	input = ""
	script.Parent.Screen.SurfaceGui.TextLabel.Text = ""
	print("CLEARED")
end

script.Parent.Clear.ClickDetector.MouseClick:Connect(Decline)

does anyone see an error in the Accept() local function?

Try using tostring(). The if statement is probably false because the number is not also a string. Could be wrong though.

1 Like

how do I use :tostring()? I never used it before

Just put the correctcode in the middle of the brackets. Works with numbers and etc.

if input == tostring(CorrectCode) then
1 Like

You are trying to compare a number to a string, which is why it displays “wrong code”, i think

1 Like