Trouble with key system

Guten tag! I am currently trying to create a key system. How it works right now is that, you enter a 4-digit-key into the textbox, then press a button. If the code is correct, the code will print that it is correct. The same happens if it’s incorrect. (It’s going to open a GUI but I haven’t made said GUI yet.)

(My video won’t upload, heh)

Code:

local button = script.Parent -- the button that is clicked
local keyBox = button.Parent.TextBox -- the box where the key is entered
local keyEntered = button.Parent.TextBox.Text -- the key that is entered
local key = 8888 -- the key that needs to be entered

function clicked()
	print("Button clicked")
	if keyEntered == key then
		keyEntered = key
		print("Key is correct!")
	else
		print("Key is incorrect!")
	end
end

button.MouseButton1Click:Connect(clicked)

while true do
	wait (1)
	print(keyEntered)
end

Thank you in advance! :slight_smile:

function clicked()
	print("Button clicked")
	if keyBox.Text == key then
		keyEntered = key
		print("Key is correct!")
	else
		print("Key is incorrect!")
	end
end

Thanks, but that didn’t make it work. :sweat_smile:. On the bright side, after editing my script to look like that, it is now printing what was in the textbox (It was printing blank).

I believe what the issue here is, is that you haven’t changed the key variable into a string
What I mean by that, is this line here:

Is defined as a Number while this

local key = "8888"

Is a String, think of it like this

  • Strings can be pretty much anything, whether it be a sentence or a sequence of numbers (Which is why they’re encased in quotation marks)

    • Example 1: "Hello, I am a string value!"
    • Example 2: "The square root of 4 is 2!"
  • Numbers however can only be (As you would assume) defined as numeric values

    • Example 1: 20
    • Example 2: 50 / 5 - 20

Think of Number variables as like pretty much Math
To fix your issue, just simply change the key variable to this:

local key = "8888"

It should work, and if it doesn’t try debugging it a bit more :thinking:

1 Like

Thank you so much! It now works! :smiley:

1 Like

Anytime! I also hope you learned a bit about the differences between String & Number values!

1 Like