Unable to get the textbox's text for a code system

  1. I want the player to be able to input a four-digit code into a textbox that is on a part and for the script to check if the code is correct or not.

  2. It only says it’s incorrect every single time.

  3. So far I’ve tried changing how I called the textbox.text to
    local Input = tostring(script.Parent.Parent.TextBox.Text) and, local Input = script.Parent.Parent.TextBox.Text I’ve also tried changing the way I had the passcode written. My previous version were:local CurrentCode = "4927" or local CurrentCode = 4927 and even local CurrentCode = tonumber(4927). I’ve tried using an attribute with a number value as well to see if the player changed the text it would change the attribute and check that instead since I couldn’t get the value, but it didn’t work either.
    (I also decided to print the Input that the player put in the textbox.text to see what the script is checking and it prints blank onto the server so I’m 100% sure it’s the way I’m trying to grab the text.)
    Here is the current code I have that is one of the random solutions I’ve tried.

local Input = tostring(script.Parent.Parent.TextBox.Text)
local Button = script.Parent

script.Parent.Parent.TextBox.PlaceholderText = "Enter Code"

local CurrentCode = "4927"

Button.MouseButton1Click:Connect(function()
	print("Checking passcode..")
-- This is where I'm talking about how I printed the Input, and it ended up printing blank with every method I tried.
	print(Input)
	if Input == CurrentCode then
		print("Correct")
	else 
		print("Incorrect")
	end

end)

For reference I have a part with ScreenGui, then with a TextBox and TextButton as children, and the script is in the TextButton.

1 Like

That’s because Input is still equal to what the Text was when the script first ran. You need to move the local input code into the .MouseButton1Click function for it to work.

It’s still printing nothing in the output, and printing incorrect.

I think you need the user input service for this and not clicked event.

Is this what your code looks like?

local Button = script.Parent
script.Parent.Parent.TextBox.PlaceholderText = "Enter Code"

local CurrentCode = "4927"

Button.MouseButton1Click:Connect(function()
	print("Checking passcode..")
    local Input = tostring(script.Parent.Parent.TextBox.Text)
	print(Input)
	if Input == CurrentCode then
		print("Correct")
	else 
		print("Incorrect")
	end

end)

UserInputService is the better option, but it’s not required as the output was able to print “Incorrect” which means the function works but the print(Input) not working means the Input is incorrect somewhere.

Yeah that’s what the code looks like

I didn’t fully read your post but skimmed it; are you using this on a ServerScript? If so, you need to change it to a LocalScript.

Using a local script makes it not work anymore. Nothing pops up in the output.

After playing around with the script properties I just changed the RunContext to Client which just makes the script local. I’m not sure why it didn’t work when I copied the code onto another local script and disabled the server script. I’m not sure why this solved the problem lol.

Okay…? Different qustion, why use a ScreenGUI on a part instead of a SurfaceGUI?

I did use a Surface Gui I just wasn’t thinking when I typed lol

I think it’s not working because “Input” is only saved as what the text box’s text is when the code first runs, which is nothing.

Try putting the Input variable inside the function above the “Checking passcode…” print.

Got it.

A SurfaceGUI will run the code from the Server; however, because the client types the Text into the TextBox object, the Text is still Client-Sided and not Server-Sided. Your solution works, but for it to be protected, you should keep RunContext to Server and send the Client Text to the Server.

For this to work, you need to get the code to work on a Local Script and then use RemoteEvents or RemoteFunctions to send/send and return data from/to the Client and Server.

As an Edit and Note, anything dealing with UI should be held and dealt with on the Client and not on the Server. If you need to run checks for data or get data, then you use RemoteFunctions/Events to run code the Client needs on the Server.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.