Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago
Edited 2 years ago

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

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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)
Ad

Answer this question