Attempt to index nil with 'FindFirstChild'

  1. I’m making a leaderboard to update and sync with leaderstats so I am trying to define the leaderstats.

  2. The issue is that its giving me an error saying my code tried to index nothing with FindFirstChild but the code leads straight to the object.

function Update()
	local frames = workspace.Leaderboard.SurfaceGui.ScrollingFrame:GetChildren()
	for v,v in pairs(frames) do
		if v:IsA("Frame") then
			v:Destroy()
		end
	end
	
	local leaderstats = game.Players.LocalPlayer:FindFirstChild("leaderstats")
	local donation = leaderstats and leaderstats:FindFirstChild("Donation")
	
	for _,plr in next, game:GetService("Players"):GetPlayers() do
		pcall(function()
			lbData:UpdateAsync(plr.UserId, function() return plr:FindFirstChild("leaderstats"):WaitForChild("Donation").Value end) 
		end)
	end

Screen Shot 2021-04-13 at 12.31.35 PM

Anybody have a possible solution to this?

it’s this line right?
local leaderstats = game.Players.LocalPlayer:FindFirstChild("leaderstats")
I think the issue is that game.Players.LocalPlayer is nil since you seem to be using a script instead of a local script.
Either remove those lines or do that inside that loop and replace game.Players.LocalPlayer with plr

But what is line 15?

It’s because this is a server script trying to access a local player, you need to access it through a local script not a server script

As everyone else mentioned, it’s cause you’re using LocalPlayer in a server script, you don’t use those for anything, so I think you can just remove them

function Update()
	local frames = workspace.Leaderboard.SurfaceGui.ScrollingFrame:GetChildren()
	for v,v in pairs(frames) do
		if v:IsA("Frame") then
			v:Destroy()
		end
	end
	
	for _,plr in next, game:GetService("Players"):GetPlayers() do
		pcall(function()
			lbData:UpdateAsync(plr.UserId, function() return plr:FindFirstChild("leaderstats"):WaitForChild("Donation").Value end) 
		end)
	end

Hes probably using them for more of the code but only showed us this portion I believe the method I gave him should suffice though

You suggested using a local script but he’s obviously using data stores which would not work there.
Tho removing those lines completely is also not a great solution, if OP could give us more info we could help.

There we go removed the post about local script

1 Like