I'm trying to script a door which requires a key to open it. Here is the script:
01 | script.Parent.Touched:connect( function (hit) |
02 | if hit.Parent ~ = nil then |
03 | local plr = game.Players:GetPlayerFromCharacter(hit.Parent) |
04 | if plr.Backpack:FindFirstChild( 'Temple Key' ) then -- The problem!!! |
05 | script.Parent.Transparency = 0.7 |
06 | script.Parent.CanCollide = false |
07 | plr.Backpack [ 'Temple Key' ] :Destroy() |
08 | wait( 5 ) |
09 | script.Parent.Transparency = 0 |
10 | script.Parent.CanCollide = true |
11 | end |
12 | end |
13 | 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?
01 | script.Parent.Touched:connect( function (hit) |
02 | local human = hit.Parent:findFirstChild( "Humanoid" ) |
03 | if (human ~ = nil ) then -- check if it is a player |
04 | local plr = game.Players:GetPlayerFromCharacter(hit.Parent) |
05 | if plr.Backpack:FindFirstChild( 'Temple Key' ) then |
06 | script.Parent.Transparency = 0.7 |
07 | script.Parent.CanCollide = false |
08 | plr.Backpack [ 'Temple Key' ] :Destroy() |
09 | wait( 5 ) |
10 | script.Parent.Transparency = 0 |
11 | script.Parent.CanCollide = true |
12 | end |
13 | end |
14 | end ) |