I made a script for Roblox Studio that acts as a computer where you need to input a password. However, When I input a passcode, nothings changes in the TextLabel. I've checked, and all the variables are set to the right thing.
Here is my code:
-- Define references to the TextBox and TextLabel objects
local textBox = script.Parent.Input
local textLabel = script.Parent.Output
-- Define passcode
local passcode = "Ol8s%w_"
-- Define function to update the TextLabel text
local function updateTextLabel()
if textBox.Text == passcode then
textLabel.Text = "Correct Passcode!"
elseif textBox.Text == "" then
textLabel.Text = "Please enter correct passcode to continue"
else
textLabel.Text = "Incorrect, try again"
wait(3)
textLabel.Text = ""
end
end
-- Set initial TextLabel text
textLabel.Text = "Please enter correct passcode to continue"
-- Set up event listener for TextBox text changes
textBox:GetPropertyChangedSignal("Text"):Connect(updateTextLabel)
Once you input the right password in a textbox, the text label should say "Correct Passcode" and if you input the wrong passcode, is should say "Incorrect, Try Again" for 3 seconds, then go back to its default message. I've tried multiple ways to fix this. Please help me.
Here is the setup:
The problem here is that assuming that this code is in a server script, the content of the TextBox is not replicated to the server and therefore the server does not register the change. To fix this you should add a LocalScript that fires a RemoteEvent with the password when the text changes.
LocalScript:
-- Define references to the TextBox and TextLabel objects
local textBox = script.Parent.Input
-- Define function to update the TextLabel text
local function updateTextLabel()
script.Parent.Password:FireServer(textBox.Text)
end
-- Set up event listener for TextBox text changes
textBox:GetPropertyChangedSignal("Text"):Connect(updateTextLabel)
ServerScript:
-- Define references to the TextBox and TextLabel objects
local textLabel = script.Parent.Output
-- Define passcode
local passcode = "Ol8s%w_"
-- Define function to update the TextLabel text
local function updateTextLabel(player, password)
if password == passcode then
textLabel.Text = "Correct Passcode!"
elseif password == "" then
textLabel.Text = "Please Insert Correct Passcode To Continue"
else
textLabel.Text = "Incorrect, Try Again"
wait(3)
textLabel.Text = ""
end
end
-- Set initial TextLabel text
textLabel.Text = "Please Insert Correct Passcode To Continue"
-- Listen to the remoteEvent
script.Parent.Password.OnServerEvent:Connect(updateTextLabel)
Structure:
The one thing to be aware with an implementation like this is the client will fire the RemoteEvent every time the text changes. I would recommend having a button the user presses to send the password to prevent RemoteEvent spam.