Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Workspace.ExitDoor.Script:7: attempt to index nil with 'FindFirstChild'? Error

What i'm trying to do here is check if the players has the key that is needed to open a door.

Here's the code:

local KeyToched = script.Parent.KeyToched
local Key = script.Parent.Key
local NeededKey = Key.Value
local Door = script.Parent.Parent.ExitDoor

game.Players.PlayerAdded:Connect(function(plr)
	if plr.Backpack:FindFirstChild(NeededKey) or plr.Character:FindFirstChild(NeededKey) then
		Door.Touched:Connect(function(player)
			if player:FindFirstChild("Humanoid") then
				Door.Padlock.Anchored = false
				Door.Padlock.CanCollide = false
			end
		end)
	end
end)

I don't know how to possibly fix it...

and the error is what the title says.

1 answer

Try to use this: (Also add the script as a server script Inside of ServerScriptService)

local ToolName = "Key"

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		if Player.Backpack:FindFirstChild(ToolName) then
			print("The user has a key!")
		else
			print("User has no key")
		end
	end)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.