Door code issue

While developing a system for entering a four-digit code to open a door, I encountered an issue: when trying to retrieve the value from TextBox.Text , the string "TextBox" is returned instead of the user’s input. This causes the comparison with the correct code to always fail, resulting in the message: “Incorrect code. Try again.”

image

local TweenService = game:GetService("TweenService")
local door = script.Parent
local proximityPrompt = door:FindFirstChild("ProximityPrompt")
local window = door:FindFirstChild("Window2")
local number = door:FindFirstChild("Number2")

local correctCode = "0000"

local isOpen = false
local originalDoorPosition = door.Position
local offset = Vector3.new(0, 15, 0)

local function openDoorAndParts()
    if not isOpen then
        isOpen = true
        door.Position = door.Position + offset
        task.wait(5)
        door.Position = originalDoorPosition
        isOpen = false
    end
end

local function checkCode(player, enteredCode)
    enteredCode = enteredCode:match("^%s*(.-)%s*$")
    print("Entered Code (trimmed):", enteredCode)
    print("Correct Code:", correctCode)

    if enteredCode == correctCode then
        print("Correct code entered! Opening the door.")
        openDoorAndParts()
    else
        warn("Incorrect code. Try again.")
    end
end

proximityPrompt.Triggered:Connect(function(player)
    local screenGui = Instance.new("ScreenGui")
    screenGui.Parent = player:WaitForChild("PlayerGui")

    local textBox = Instance.new("TextBox")
    textBox.Size = UDim2.new(0, 200, 0, 50)
    textBox.Position = UDim2.new(0.5, -100, 0.5, -25)
    textBox.PlaceholderText = "Enter the code"
    textBox.Parent = screenGui

    local submitButton = Instance.new("TextButton")
    submitButton.Size = UDim2.new(0, 200, 0, 50)
    submitButton.Position = UDim2.new(0.5, -100, 0.6, -25)
    submitButton.Text = "Submit"
    submitButton.Parent = screenGui

    submitButton.MouseButton1Click:Connect(function()
        local enteredText = textBox.Text
        print("Submit button clicked!")
        print("TextBox value (user input):", enteredText)
        checkCode(player, enteredText)
        screenGui:Destroy()
    end)
end)

You can’t get the Text from the player because it’s a Gui and Guis only exist locally, you have to use a RemoteEvent inside a LocalScript to retrieve the text

This is happening because of how the client and the server process information, you are creating the text box on the client and then when the player enters the text on the client side the text has been edited locally, but on the server the text box was never edited.

You could edit your server script to fire a remote event to create the gui locally.
and then fire the remote event again once you have confirmed the code is correct

Server script:

local TweenService = game:GetService("TweenService")
local door = script.Parent
local proximityPrompt = door:FindFirstChild("ProximityPrompt")
local window = door:FindFirstChild("Window2")
local number = door:FindFirstChild("Number2")

local correctCode = "0000"

-- ///
local remoteEvent = game.ReplicatedStorage.RemoteEvent
-- ///

local isOpen = false
local originalDoorPosition = door.Position
local offset = Vector3.new(0, 15, 0)

local function openDoorAndParts()
	if not isOpen then
		isOpen = true
		door.Position = door.Position + offset
		task.wait(5)
		door.Position = originalDoorPosition
		isOpen = false
	end
end

proximityPrompt.Triggered:Connect(function(player)	
	remoteEvent:FireClient(player)
end)

remoteEvent.OnServerEvent:Connect(openDoorAndParts)

Local script:

local remoteEvent = game.ReplicatedStorage.RemoteEvent
local player = game.Players.LocalPlayer

local correctCode = "0000"
local isOpen = false

function checkCode(player, enteredCode)
	enteredCode = enteredCode:match("^%s*(.-)%s*$")
	print("Entered Code (trimmed):", enteredCode)
	print("Correct Code:", correctCode)

	if enteredCode == correctCode then
		print("Correct code entered! Opening the door.")
		remoteEvent:FireServer()
	else
		warn("Incorrect code. Try again.")
	end
end

remoteEvent.OnClientEvent:Connect(function()
	local screenGui = Instance.new("ScreenGui")
	screenGui.Parent = player:WaitForChild("PlayerGui")

	local textBox = Instance.new("TextBox")
	textBox.Size = UDim2.new(0, 200, 0, 50)
	textBox.Position = UDim2.new(0.5, -100, 0.5, -25)
	textBox.PlaceholderText = "Enter the code"
	textBox.Parent = screenGui

	local submitButton = Instance.new("TextButton")
	submitButton.Size = UDim2.new(0, 200, 0, 50)
	submitButton.Position = UDim2.new(0.5, -100, 0.6, -25)
	submitButton.Text = "Submit"
	submitButton.Parent = screenGui

	submitButton.MouseButton1Click:Connect(function()
		local enteredText = textBox.Text
		print("Submit button clicked!")
		print("TextBox value (user input):", enteredText)
		checkCode(player, enteredText)
		screenGui:Destroy()
	end)
end)

2 Likes

Thank you so much ////////////////////

1 Like