Attempt to index nil with "FindFirstChild"

  1. What do you want to achieve?
    I want to give players a badge when they touch a part if they have a certain value of their IntValue in their player instance.

  2. What is the issue?
    I get this error:
    image

  3. Did you look for solutions on the Developer Hub?
    Yes.

local db = {}
local badgeID = 2129940979
local bs = game:GetService("BadgeService")
script.Parent["Meshes/Token_Token_Base"].Touched:Connect(function(hit)
	local char = hit.Parent
	local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
	if plr:FindFirstChild("Stars").Value >= 5 then -- error on this line
		table.insert(db,char.Name)
	end
	if table.find(db, char.Name) ~= nil then
		table.insert(db,char.Name)
		plr.Stars.Value += 1
		if plr.Stars.Value == 5 then
			bs:AwardBadge(plr.UserId, badgeID)
		end
	end
end)
local db = {}
local badgeID = 2129940979
local bs = game:GetService("BadgeService")
script.Parent["Meshes/Token_Token_Base"].Touched:Connect(function(hit)
	local char = hit.Parent
	local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
	if not plr then return end --this is what I changed (added)

	if plr:FindFirstChild("Stars").Value >= 5 then -- error on this line
		table.insert(db,char.Name)
	end
	if table.find(db, char.Name) ~= nil then
		table.insert(db,char.Name)
		plr.Stars.Value += 1
		if plr.Stars.Value == 5 then
			bs:AwardBadge(plr.UserId, badgeID)
		end
	end
end)

Now I get no error, but still nothing happens. It appears as though plr is not a player somehow.

Have you forget to put the Folder’s name right before the Stars Value?
Most of the people use the Leaderstats To Their Folder Name On Their Data.

That’s how your script should be:

local db = {}
local badgeID = 2129940979
local bs = game:GetService("BadgeService")
script.Parent["Meshes/Token_Token_Base"].Touched:Connect(function(hit)
	local char = hit.Parent
	local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
	if plr:FindFirstChild("leaderstats").Stars.Value >= 5 then -- error on this line
		table.insert(db,char.Name)
	end
	if table.find(db, char.Name) ~= nil then
		table.insert(db,char.Name)
		plr.leaderstats.Stars.Value += 1
		if plr.leaderstats.Stars.Value == 5 then
			bs:AwardBadge(plr.UserId, badgeID)
		end
	end
end)

It isn’t in a folder, the value is just in the player.

Is it a Server Script?

If it’s a server Script, consider changing it To A LocalScript.

I don’t think LocalScripts would work in Workspace.

plr might not always exist. Add this if statement before that one: if not plr then return end.

I’ll be sure to add that in, but either way this error only comes when my character touches it. In this case, plr is a player and yet its saying that it is nil.

I’ve added it in, and as predicted, it still doesn’t work.

Did you see if it pass or not? try this and tell me what prints out.

local db = {}
local badgeID = 2129940979
local bs = game:GetService("BadgeService")
script.Parent["Meshes/Token_Token_Base"].Touched:Connect(function(hit)
	local char = hit.Parent
	local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
	if not plr then return end --this is what I changed (added)

    print("There is a player")

	if plr:FindFirstChild("Stars").Value >= 5 then -- error on this line
		table.insert(db,char.Name)
		print("Found stars.")
	end
	if table.find(db, char.Name) ~= nil then
		print("Found in table")
		table.insert(db,char.Name)
		plr.Stars.Value += 1
		if plr.Stars.Value == 5 then
		    print("Stars is equal to 5")
			bs:AwardBadge(plr.UserId, badgeID)
		end
	end
end)

All it prints out is “There is a player”.

I had to end up putting the values in ReplicatedStorage instead of in the player instance for it to work.

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