FindFirstChild("Humanoid") Isn't Working For My Collecting Script

I made a coin collecting script, but when the coin touches something that’s not a player the script still runs which is why I get this error.

14:21:34.142 - Workspace.ElixirDrop.ElixirDropCollect:14: attempt to index nil with ‘leaderstats’

local elixir = script.Parent
    local currency = "Elixir"
    local amt = 1 
   
    local d = true

    elixir.Touched:Connect(function(hit)
   
      if hit.Parent:FindFirstChild("Humanoid") and d then
         d = false

        local plr = game.Players:FindFirstChild(hit.Parent.Name)

        plr.leaderstats:FindFirstChild(currency).Value =  plr:FindFirstChild("leaderstats"):FindFirstChild(currency).Value + amt

        elixir:Destroy()
      end
    end)

Try using

local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

instead of

local plr = game.Players:FindFirstChild(hit.Parent.Name)
1 Like

You aren’t taking into account that FindFirstChild will return nil if the child doesn’t exist. You might have an NPC or something that has a humanoid but isn’t a player. Try checking if plr exists before indexxing it.

3 Likes

I don’t have any NPC’s though

charr

Have you tried what I mentioned above?

The coin is in server script storage could that be affecting it?

This script is the coin’s parent

local Debris = game:GetService("Debris")

local spawner = game.Workspace.Spawner

local elixir = script.ElixirDrop

local r = 250

while wait() do

local elixirclone = elixir:Clone()

local x,z = spawner.Position.X,spawner.Position.Z

x,z = math.random(x - r,x + r),math.random(z - r,z + r)

local pos = Vector3.new(x,spawner.Position.Y,z)

elixirclone.Parent = workspace

elixirclone.Position = pos

Debris:AddItem(elixirclone, 10)

end

yeah I still get the same result

And you’re sure that “leaderstats” exists?

That’s inside ServerScriptService…

If you’re talking about the leader stats script then yes

In your first script, you’re referring to something called “leaderstats”. Does that thing actually exist?

Hmm… are these custom folders created by a localscript?

No it’s created by the script in server script service called leaderstats

local data_store_name = "ICE" 
local ds = game:GetService("DataStoreService"):GetDataStore(data_store_name)

local function join(plr)


    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    
    local gold = Instance.new("NumberValue")
    gold.Name = "Gold"
    gold.Value = 0
    gold.Parent = leaderstats
    
    local elixir = Instance.new("NumberValue")
    elixir.Name = "Elixir"
    elixir.Value = 0
    elixir.Parent = leaderstats

    local darkElixir = Instance.new("NumberValue")
    darkElixir.Name = "Dark Elixir"
    darkElixir.Value = 0
    darkElixir.Parent = leaderstats

You connected the function to game.Players.PlayerAdded, right?

game.Players.PlayerAdded:Connect(join)

It’s because you’re increasing the value of the player when it doesn’t exist in “game.Players”

Add this then put the increase inside it.
“if plr then
-increase
end”

Okay so I added it like this

    local elixir = script.Parent
    local currency = "Elixir"
    local amt = 1 
   
    local d = true

    elixir.Touched:Connect(function(hit)
   
      if hit.Parent:FindFirstChild("Humanoid") and d then
         d = false
		
        local plr = game.Players:FindFirstChild(hit.Parent)
if plr then
        plr("leaderstats"):FindFirstChild(currency).Value =  plr:FindFirstChild("leaderstats"):FindFirstChild(currency).Value + amt
end
        elixir:Destroy()
      end
    end)

But I get this error

15:43:20.694 - Workspace.ElixirDrop.ElixirDropCollect:9: attempt to index nil with 'FindFirstChild'

I’m stumped. This should work. The problem might be within your spawner?

Edit: It ain’t the spawner.

Redo the script, just make sure it touches and removes. Don’t add more then you need for testing, do it later. Just make sure it touches, and removes.

You forgot to give it the name of hit.Parent

Also why not use :GetPlayerFromCharacter. It is meant specifically for these types of cases.

local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
1 Like