I'm trying to script a door which requires a key to open it. Here is the script:
script.Parent.Touched:connect(function(hit)
if hit.Parent ~= nil then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr.Backpack:FindFirstChild('Temple Key') then -- The problem!!!
script.Parent.Transparency = 0.7
script.Parent.CanCollide = false
plr.Backpack['Temple Key']:Destroy()
wait(5)
script.Parent.Transparency = 0
script.Parent.CanCollide = true
end
end
end)
When I touched the door (with and without the key), the output stated this error: 16:56:04.906 - Workspace.Union.Script:4: attempt to index local 'plr' (a nil value)
How am I able to fix line 4 of the script above?

2 comments
You're assuming that some brick didn't just hit the part. Line 2 would tell the script to proceed, however line 3 will return nil if the brick's parent isn't a character model. Therefore line 4 will give the mentioned error.
so how should I fix it?