Attempt to index nil with 'Parent'

  1. What do you want to achieve?
  • I want to remove tools from player inventory when joining and save them inside a folder.
  1. What is the issue?
  • Output displays this error: attempt to index nil with ‘Parent’.
local RS = game:GetService("ReplicatedStorage")
local Folder = RS:WaitForChild("ObtainableGuns")

	if Player.Backpack then
		local AKM = Player.Backpack:FindFirstChild("AKM")
		local AUGA3 = Player.Backpack:FindFirstChild("AUG A3")
		local AmmoBox = Player.Backpack:FindFirstChild("AmmoBox")
		local Binoculars = Player.Backpack:FindFirstChild("Binoculars")
		local DesertEagle = Player.Backpack:FindFirstChild("Desert Eagle")
		local MP5K = Player.Backpack:FindFirstChild("MP5K")
		local Shield = Player.Backpack:FindFirstChild("Shield")
		local TTIBENELLI = Player.Backpack:FindFirstChild("TTI Benelli M2")
		local USP = Player.Backpack:FindFirstChild("USP")
		AKM.Parent = Folder --The error displays here
		AUGA3.Parent = Folder
		AmmoBox.Parent = Folder
		Binoculars.Parent = Folder
		DesertEagle.Parent = Folder
		MP5K.Parent = Folder
		Shield.Parent = Folder
		TTIBENELLI.Parent = Folder
		USP.Parent = Folder

Okay, this code is rather messy. I would recommend cleaning it up with a for loop. As for your error, one of the tools does not exist.


for _, T in next, Player.Backpack:GetChildren() do
T.Parent = Folder

You could also just use an array when the player joins.

local PlayerTools = {}

PlayerTools[UserId] = {}

for _, T in next, Player.Backpack:GetChildren() do
PlayerTools[UserId][T] = Amount or 1

Now, as mentioned before your error is attempting to index parent with nil.

It looks like this:

nil.Parent = Folder 

When it should look like

instance.Parent = Folder

It means “AKM” is nil. You are trying to set a parent to nil

If this line doesn’t find AKM, then AKM will = nil