Error 10:28:59.275 - Workspace.Script:15: attempt to index nil with 'FindFirstChild'?

I am making it so if I touch a part with a tool it gives money but it keeps giving that error, script.

function CopperOres()
	local CopperOre = Instance.new("Part",workspace)
	CopperOre.Anchored = true
	CopperOre.CFrame = CFrame.new(math.random(-50, -30), 1, math.random(-50, -3))
	CopperOre.Shape = Enum.PartType.Ball
	CopperOre.TopSurface = Enum.SurfaceType.Smooth
	CopperOre.BottomSurface= Enum.SurfaceType.Smooth
	CopperOre.Size = Vector3.new(3, 3, 3)
	CopperOre.BrickColor = BrickColor.new("Copper")
	CopperOre.Material= Enum.Material.Pebble
	CopperOre.Touched:connect(function(p)
		if p.Parent.Parent.Name == "Copper Pickaxe" then
			CopperOre:Destroy()
			local player = game.Players.LocalPlayer
			local leaderstats = player:FindFirstChild("leaderstats")
			local Coins = leaderstats and leaderstats:FindFirstChild("Money")
			Coins.Value = Coins.Value + 10
		end
	end)
end
while wait(math.random(12,15)) do
	CopperOres()
end
2 Likes

You cannot access game.Players.LocalPlayer from a normal Script. You can only access it from a LocalScript. Therefore when you wrote:

it gives you an error, because player is nil (because game.Players.LocalPlayer is nil)

1 Like

is there a way to define a player without

local function onPlayerAdded(player)

Yes, there is

CopperOre.Touched:connect(function(p)
		if p.Parent.Parent.Name == "Copper Pickaxe" then
			CopperOre:Destroy()
			local player = game.Players:GetPlayerFromCharacter(p.Parent) -- use this function to get the player from character 
			local leaderstats = player:FindFirstChild("leaderstats")
			local Coins = leaderstats and leaderstats:FindFirstChild("Money")
			Coins.Value = Coins.Value + 10
		end
	end)
1 Like

What you would want to do is to get the Player from the touch event, like so:

    CopperOre.Touched:connect(function(p)
                local humanoid = (p.Parent.Parent.Parent):FindFirstChild("Humanoid")
                if humanoid and p.Parent.Parent.Name == "Copper Pickaxe" then
                    CopperOre:Destroy()
                    local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
	                local leaderstats = player:FindFirstChild("leaderstats")
		            local Coins = leaderstats and leaderstats:FindFirstChild("Money")
		            Coins.Value = Coins.Value + 10
                end
	end)

i still get error [10:36:46.973 - Workspace.Script:15: attempt to index nil with ‘FindFirstChild’])

That wouldn’t work because Touched returns the part that touched CopperOre, not the player which touched it.

1 Like

Try to add some checks to see if the part touching is a player

1 Like

I now get 10:39:55.149 - Workspace.Script:14: attempt to index nil with ‘Parent’

try this

local humanoid = (p:FindFirstAncestorOfClass("Model") or p.Parent):FindFirstChild("Humanoid")
1 Like

in line 2
(p.parent.parent.parent) --workspace
removing one parent would be
(p.parent.parent) --character

1 Like

error 10:44:16.390 - Workspace.Script:18: attempt to index nil with ‘Value’

Change this to:

local Coins = leaderstats:FindFirstChild("Money")
if Coins then Coins.Value = Coins.Value + 10 end

Note: Also make sure the Coins actually exists in the first place otherwise it will never be able to add to it

1 Like

maybe leaderstats and coins are not loaded in yet
since they are created by another script.
try:
WaitForChild()
instead of:
FindFirstChild()

1 Like

I am getting no error but it isn’t changing the value of coins

That means that leaderstats.Money doesnt exist. Are you sure it is called “Money”?

1 Like

I used

local Coins = leaderstats:FindFirstChild("Money")

for the leaderstat I used

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
 
	local Money = Instance.new("IntValue")
	Money.Name = "Gold"
	Money.Value = 0
	Money.Parent = leaderstats
end
 
game.Players.PlayerAdded:Connect(onPlayerJoin)

when I did that I get no errors but nothing happens

You called it “Gold”, not “Money”.
Change it to this:

local Coins = leaderstats:FindFirstChild("Gold")
1 Like