Attempt to index nil with "Parent"

Why does this error pop up?

local character = script.Parent
local humanoid = character:FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(character)
local backpack = player:WaitForChild("Backpack")
local limit = 5
local limit2 = 10
local hadToolEquipped = false



humanoid.Died:Connect(function()
	for i, tool in pairs(character:GetChildren()) do
		if tool:IsA("Accessory") and tool:FindFirstChild("Handle") and tool:GetAttribute("Armor") then
			hadToolEquipped = true
			tool.Parent = game.ReplicatedStorage
		end
	end
end)



player.CharacterRemoving:Connect(function(character)
	for i, tool in ipairs(game.ReplicatedStorage:GetChildren()) do
		if tool:IsA("Accessory") and tool:FindFirstChild("Handle") and tool:GetAttribute("Armor") then
			task.spawn(function()
				tool.Parent = workspace
				tool.Handle.CFrame = character.HumanoidRootPart.CFrame
				task.wait(15)
				if tool.Parent == workspace then
					tool:Destroy()
				end
			end)
		end
	end
	if hadToolEquipped ==true then
		local equippedTool = game.ReplicatedStorage:FindFirstChildOfClass("Accessory")
		equippedTool.Parent = game.Workspace
		equippedTool.Handle.CFrame = character.HumanoidRootPart.CFrame
	end
end)

Error:

Workspace.SpiderLeander1.Script:37: attempt to index nil with 'Parent'
1 Like

The script cant find a Accessory in game.ReplicatedStorage so its trying to Parent nothing to game.Workspace

1 Like

that just means it can’t find the object you are trying to set the parent of.
It could be the equipped tool so i suggest added in a check by doing

`if equippedTool then`
  equippedTool.Parent = game.Workspace
  equippedTool.Handle.CFrame = character.HumanoidRootPart.CFrame
end

Change that code to this:

if hadToolEquipped and game.ReplicatedStorage:FindFirstChildOfClass("Accessory") then
    local equippedTool = game.ReplicatedStorage:FindFirstChildOfClass("Accessory")
    equippedTool.Parent = game.Workspace
    equippedTool.Handle.CFrame = character.HumanoidRootPart.CFrame
end