Attempting to index nil with a global variable

Hi, developers!

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?

Any help would be hugely appreciated!

5 Likes

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 :slightly_smiling_face:

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.

2 Likes

Well, there are multiple ways you can do this depending on how you want your word to be retrieved by the players

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.

1 Like

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

Sure thing!

Here’s where the event is fired: (local script inside of code screen)
image

Here’s where the event is set up:

2 Likes

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?

The comparison should ideally be done on the server only, not on the client. I’ll write an example if you’d like

1 Like

I would much appreciate an example. I’m not really familiar with server-client stuff.

1 Like

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

1 Like

Thanks so much for the help! I didn’t want to have to stay up longer than I needed to trying to solve this.

1 Like

You’re welcome, if you need more information on how my example works feel free to message me :slightly_smiling_face::+1:

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