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.
It only says it’s incorrect every single time.
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.
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.
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.
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.
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.