Attempt to index nil with 'FindFirstChild'?

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

  1. **I am currently attempting to make a system where when a player loads into the game it clones a “Factory” from ReplicatedStorage and renames the cloned Factory to their UserId (Helps identify the player’s Factory easier for my TP System), then I have a teleporting system that checks all the cloned factories and checks if the cloned factory contains a part named (*LocalPlayer’sID) and if it does then it teleports the Player to his individual factory. **

  2. It is saying its failed to index nil with ‘FindFirstChild’ when referencing checking if the child of the “DefaultFactory” is the player’s UserID

(“FactoryLoader”) LOCATED IN “ServerScriptService” V

local dFac = game:GetService("ReplicatedStorage"):WaitForChild("Factories").DefaultFactory
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("FactoryDS")
local Players = game:GetService("Players")
local Nodes = game:GetService("Workspace"):WaitForChild("FactoryNodes")

Players.PlayerAdded:Connect(function(player)
	local factoryLoaded = false
    for _, location in Nodes:GetChildren() do
		if not location:FindFirstChild("DefaultFactory") then
			if factoryLoaded then break end
            local success, data = pcall(function()
                return DataStore:GetAsync(tostring(player.UserId))
            end)
            if success and data and data["OwnedFactory"] then
                local LoadFac = dFac:Clone()
				LoadFac.Parent = location
				LoadFac:PivotTo(location.CFrame)
				LoadFac.DefaultFactory.Name = tostring(player.UserId)
				factoryLoaded = true
                print("FactoryPerPlayerLoaded")
            end
        end
    end
end)

(“Script”) LOCATED IN Workplace under “Static” V

local Players = game:GetService("Players")
local Factories = game:GetService("Workspace").FactoryNodes
local TouchPart = script.Parent

TouchPart.Touched:Connect(function(touched)
	if touched.Parent:IsA("Model") and touched.Parent:FindFirstChild("Humanoid") then
		local Player = Players:GetPlayerFromCharacter(touched.Parent)
		if Player then
			local FacNodes = Factories:GetChildren()
			for _, Node in FacNodes do
				if Node:FindFirstChild("DefaultFactory"):FindFirstChild(tostring(Player.UserId)) then
					local FacTeleporter = Node:FindFirstChild("DefaultFactory").ReplicatedTeleporter
					local HumanoidRP = Player.Character:FindFirstChild("HumanoidRootPart")
					if FacTeleporter then
						if HumanoidRP then
							HumanoidRP.CFrame = FacTeleporter.CFrame
						end
					end
				end
			end
		end
	end
end)

I know this is a hefty ask but it could just be that I’m referencing or structuring my code wrong, so any help or tips are appreciated! TY

1 Like

The reason you are getting this error is because some nodes don’t have DefaultFactory inside of them. This causes the nil in the first FindFirstChild on line 11 which is then indexed again with FindFirstChild to find the object with the name of player’s UserId. This is a simple fix, however. You just need to check if DefaultFactory exists by changing this if statement:

to:

local defaultFactory = Node:FindFirstChild("DefaultFactory")
if defaultFactory and defaultFactory:FindFirstChild(tostring(Player.UserId)) then

If there is no defaultFactory, the second check is not carried out which stops the code from indexing nil.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.