Error on WaitForChild request

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? To avoid the error and make it work

  2. What is the issue? I get the error Workspace.Door.DoorFramework1:19: attempt to index nil with ‘WaitForChild’

  3. What solutions have you tried so far? Not much really. Roblox assistant also wont really help

local TweenService = game:GetService("TweenService")

local Door = script.Parent
local DoorLeft = Door.DoorLeft
local DoorRight = Door.DoorRight
local lockin = script.Parent.DoorSensorInn.Locked
local lockout = script.Parent.DoorSensorOut.Locked
local sound2 = script.Parent["door open metal"]
local granted = script.Parent["SBAudio Female Voice: Access Granted"]
local denied = script.Parent["SBAudio Female Voice: Access Denied"]
local player = game.Players.LocalPlayer

local Open = false
local DoorPos = 0
local DoorSpeed = 3
local DoorOpenTime = 1.5

local player = game.Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")  -- This is the error line

-- Clone the keycard from the StarterPack to the player's backpack
local keycard = game:GetService("StarterPack"):FindFirstChild("Keycard")
if keycard then
	keycard:Clone().Parent = backpack
end


local player = game.Players.LocalPlayer
local card = player.Backpack:FindFirstChild("Keycard")
if card then

Why is the error occurring at line 19?

If you’re running this in a regular script instead of a Local script then it wouldn’t be able to give you that player variable. Also, if this is a door and you’re going to have multiple of these, I suggest having just a single script all the doors rely on. You could have, say, that single script under StarterPlayerScript, which you could easily get hold of the player through PlayerAdded.

Alright I’ll try to use this method rq

I don’t really care about optimization right now I’ll do that for later I’m just trying to fix the backpack issue rn (and make it work on a normal script)

Hello Deer, the reason this isn’t working is because game.players.LocalPlayer can only be accessed by a LocalScript. Since this object is a door, I’m assuming you are using a Server Script.

Nik

1 Like