Trying to do a safe script

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!

1 Like

It should pick up what you type? Did you try it?
I mean… at least try… it?

2 Likes

On the TextBox.Text = Code it is code not Code.

1 Like

I haven’t tried it yet, and yes I understand that but I need to try it. I have worked with TextBoxes before but it never picks it up.

Replace

“if TextBox.Text = Code” with

“if TextBox.Text == code”

Right now, your script is trying to say "If the textbox means “Code”, then…
What it should say is "If the TextBox’s text says “code (1111)” then…

2 Likes

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.

1 Like

I understand that, the issue was with the TextBox.
I am going to test rn

2 Likes

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.

1 Like

It is in a local script. I am going to try it in a min.

1 Like

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!

1 Like

What does your updated code look like after fixing the issues listed above?

1 Like
local TextBox = script.Parent.CodeInput
local code = 1111
local Enter = script.Parent.Enter
local Close = script.Parent.Close
local Safe = game.Workspace.Part
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = Safe
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
		print("You opened it!")
	end
end)

	Close.MouseButton1Click:Connect(function()
	TextBox.Visible = false
	Close.Visible = false
	Enter.Visible = false
end)

Should I make “code” a string?

1 Like

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 :stuck_out_tongue: :

  1. Change code = 1111 to code = "1111" so it’s a string instead of a number (my preference)
  2. Cast TextBox.Text to a number - if tonumber(TextBox.Text) == code then
  3. 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 :slight_smile:

No errors in the output window.

1 Like

I see you marked it as a solution, did you get it working? :smiley:

Thank you, this helped! I don’t need it for a safe, but it is incredibly helpful for down the line.
code just had to be a string.

2 Likes