Attempt to index nil with 'FindFirstChild'

So… The title says it all. I got an error in the output.


This is my script:

local dataStore = game:GetService("DataStoreService"):GetDataStore("Researching#T103")

local RE = game.ReplicatedStorage.RemoteEvents.ResearchComplete

local researchTable = {"PurchasePercent","CocoaExchangePercent"}

RE.OnServerEvent:Connect(function(typeOfResearch, percent, plr)
	local research = plr.Researching:FindFirstChild(typeOfResearch)
	if research ~= nil then
		research.Value = percent
		print("Success")
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	pcall(function()
		local researchFolder = Instance.new("Folder",plr)
		researchFolder.Name = "Researching"
		
		-- Getting each research
		for i, v in pairs(researchTable) do
			local newPercent = Instance.new("NumberValue",researchFolder)
			newPercent.Name = v
			newPercent.Value = 0
		end
		
		local data = nil
		data = dataStore:GetAsync(plr.UserId)
		
		if data ~= nil then
			for i, v in pairs(researchFolder:GetChildren()) do
				v.Value = data[v.Name]
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	pcall(function()
		local researching = plr:WaitForChild("Researching")
		if researching then
			local data = {}
			for i, v in pairs(researching:GetChildren()) do
				data[v.Name] = v.Value
			end
			dataStore:SetAsync(plr.UserId, data)
		end
	end)
end)

Why is this happening? Please help.
3 Likes

This is happening because player.Researching doesn’t exist. Check if that exists before calling FindFirstChild() on it.

2 Likes

Now it says this:


with this code:

RE.OnServerEvent:Connect(function(typeOfResearch, percent, plr)
	local researching = plr:FindFirstChild("Researching")
	if researching ~= nil then
		local research = researching:WaitForChild(typeOfResearch)
		if research ~= nil then
			research.Value = percent
			print("Success")
		else
			error("Could not find the Type Of Research")
		end
	else
		error("Couldn't find Researching")
	end
end)

Somehow it can’t find the folder when it’s right here:

The first parameter of OnServerEvent is the player who fired it. So change this line:

RE.OnServerEvent:Connect(function(typeOfResearch, percent, plr)

to this:

RE.OnServerEvent:Connect(function(player, typeOfResearch, percent, plr)

this should fix your issue.

2 Likes

Hmm, but what about this code? (client)

local plr = game.Players.LocalPlayer
local re = game.ReplicatedStorage.RemoteEvents.ResearchComplete

script.Parent.Main.CanvasPosition = Vector2.new(2200, 1000)

script.Parent.Exit.MouseButton1Down:Connect(function()
	script.Parent.Visible = false
end)

for i, v in pairs(script.Parent.Main:GetDescendants()) do
	if v.ClassName == "TextButton" then
		v.Activated:Connect(function()
			if plr.leaderstats:WaitForChild("Crystals").Value >= v.Requirement.Value then
				local percent = v.Name
				re:FireServer(v.Parent.Name, percent, plr)
				print("Player has enough crystals")
			elseif plr.leaderstats:WaitForChild("Crystals").Value < v.Requirement.Value then
				warn("Player does't have enough crystals")
				script.Parent.Error.Visible = true
				wait(5)
				script.Parent.Error.Visible = false
			end
		end)
	end
end
1 Like

I think I found the problem. The first parameter for the OnServerEvent is the player that fired the event, when you’re finding the research itself, you’re trying to find the player that fired the event, which won’t work. Add a player parameter before the typeOfResearch parameter, and then call WaitForChild() for the name of the typeOfResearch, as WaitForChild() needs a string ads an argument.

2 Likes