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?
script.Parent.Touched:connect(function(hit) local human = hit.Parent:findFirstChild("Humanoid") if (human ~= nil ) then -- check if it is a player local plr = game.Players:GetPlayerFromCharacter(hit.Parent) if plr.Backpack:FindFirstChild('Temple Key') then 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)