I want to set a small project for myself. I want to make a simple “safe” to open. I’m not sure how to approach it, however my mind goes to something like this.
local code = 1111
local SafeDoor = game.Workspace.SafeDoor
local TextBox = script.Parent.TextBox
local Enter = script.Parent.EnterButton
local Close = script.Parent.CloseButton
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = SafeDoor
ClickDetector.MaxActivationDistance = 10000
ClickDetector.MouseClick:Connect(function()
TextBox.Visible = true
Enter.Visible = true
Close.Visible = true
end)
Enter.MouseButton1Click:Connect(function()
if TextBox.Text = Code then
--essentially the opening of the safe door
end)
Close.MouseButton1Click:Connect(function()
Enter.Visible = false
Close.Visible = false
TextBox.Visible = false
end)
I am not sure if the TextBox would pick up what I type. Please let me know how to make the input go into the TextBox. Thanks for reading and please help!
if TextBox.Text == blahblahblah will work, the code will access the Text property of your TextBox on that line, whatever is in the box will replace TextBox.Text in your code.
-- GUI Textbox.Text = "Hi"
if TextBox.Text == "Hi" then
print("Woohoo!")
end
If you’re trying to run this code and are getting errors, it’d be helpful to list those here or read them and understand what your issue is. For instance, you’re using Code instead of code you have declared at the top of your script - capitalization matters. You’re also using = instead of == for comparing the value of the Textbox’s Text to code. Another maybe not so obvious issue you will run into is comparing string with number. The Text property of a TextBox is always going to be a string (characters in “quotation marks”), even if the characters are numbers. "111" is not the same as 111. To fix this, convert TextBox.Text to a number or convert code to a string so the two data types match.
Are you doing this in a local script or server script? If this is in the server script, this script would potentially be a flaw. The server-side script doesn’t recognize any inputs from the client. If the client attempts to input 1111 in a TextBox, the server-side wouldn’t see it, therefore, it’s recommended that you use RemoteFunction and RemoteEvent to retrieve the client’s input.
Hmm, it didn’t work. I made it so if you get the code right, it will print(“You opened it!”), but when I did the exact code, nothing happened. I put it under pressing enter too!
You’re still comparing string and number. "1111" == 1111 is false, your if block won’t be entered.
There’s a few ways around this, pick your favorite :
Change code = 1111 to code = "1111" so it’s a string instead of a number (my preference)
Cast TextBox.Text to a number - if tonumber(TextBox.Text) == code then
Cast code to a string - if TextBox.Text == tostring(code) then
Do you have any errors in the Output window? They may help narrow down any other problems