I’m currently working on this system where if a player enters a correct code, a door will open. As of typing this, I’m going to try a remote event to achieve this, but thought a forum post here wouldn’t hurt.
My code errors because the game attempts to index nil with ‘FocusLost’, my textbox function.
Here’s my code:
prox.Triggered:Connect(function(plr)
local ui = plr.PlayerGui
local codeScreen = ui.CodeScreen
local main = codeScreen.Main
codeBox = main:WaitForChild('CodeBox')
local close = main.Close
print(codeScreen.Parent)
print(main.Parent)
print(codeBox.Parent)
main.Visible = true
end)
codeBox.FocusLost:Connect(function(enterPressed)
if enterPressed then
print('enter pressed')
if string.lower(tostring(codeBox.Text)) == correctWord then
print('correct')
codeBox.Text = 'Correct!'
for _, part in pairs(doorChildren) do
if not string.match(part.Name, '2') and part:IsA('BasePart') then
part.Transparency = 0
part.CanCollide = true
else
part.Transparency = 1
part.CanCollide = false
end
end
else
codeBox.Text = 'Incorrect Code'
task.wait(1.5)
if codeBox.Text == 'Incorrect Code' then
codeBox.Text = ''
end
end
end
end)
The error occurs where my code says codebox.FocusLost:Connect(function(enterPresed).
Since I’m no coding genius, I’m going to take an educated guess and say the reason this doesn’t work is because the game is trying to index variables that don’t already exist, since they haven’t been defined yet. My issue is that I don’t know how to get these variables, and if I need to move this into a local script inside of the code screen itself!
Even if I can get the FocusLost function to work, how would I replicate the correct word to the server so everyone gets the same word?
The problem is the FocusLost connection is being created before the ProximityPrompt is triggered, thus the codeBox would still be nil. This is a tricky situation to fix without creating a memory leak, but I’ll work on a solution and will update this reply once I find one
Thanks for replying! I’ve solved this problem using a remote event, the only problem that remains is me being able to replicate the correct word across the server so everyone has the same word.
The word should be generated from the list in the word list module script once the door slam event is fired, which is handled on the server. My problem is that I don’t think I can interact with that variable if my remote event doesn’t get access to that variable, since it’s in another script.
I’ll need to see how your code currently looks like to better understand this, at least where you’re using :FireServer and where you have the OnServerEvent
If I understand correctly, you’re making a code door that slams when the text inputted in the codeBox matches with the random word retrieved from the module, and you would like to verify if the word is correct on the server as well. In that case, personally I don’t recommend handling the check on the client and instead I’d check if the codeBox’s text matches the random word and also generate the random word on the server for security
There’s a part in a door model called DoorEventMarker, which slams the door when a player touches it. The text input into the CodeBox that matches the random code will unlock the door.
Adding onto your last statement, how would I compare both random words from both server and client?
You’ll need to add a RemoteFunction inside ReplicatedStorage for my example to work. Here’s the client script:
local remoteFunction = game:GetService("ReplicatedStorage"):WaitForChild("RemoteFunction") -- You can change the name of the RemoteFunction if you want
codeBox.FocusLost:Connect(function(enterPressed)
if enterPressed then
print("Submitted")
if remoteFunction:InvokeServer(codeBox.Text) then
print("Correct")
codeBox.TextColor3 = Color3.fromRGB(125, 255, 125)
codeBox.Text = "Correct!"
end
end
end)
And the server script:
local replicatedStorage = game:GetService("ReplicatedStorage")
local wordList = require(replicatedStorage.Modules.WordList)
local correctWord = wordList[math.random(#wordList)]
print("The correct word is: "..correctWord)
local remoteFunction = replicatedStorage.RemoteFunction
remoteFunction.OnServerInvoke = function(player, text)
if typeof(text) == "string" then
if string.lower(text) == correctWord then
-- Write the code to unlock the door inside this if statement
return true
end
end
end
Btw it’s very important not to accidentally delete the return true in the server script