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

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

Sorry I’m confused. What do you mean?

1 Like

As in you said hit.Parent when you meant hit.Parent.Name.

Also, just use :GetPlayerFromCharacter because it is for these kinds of situations.
https://developer.roblox.com/en-us/api-reference/function/Players/GetPlayerFromCharacter

I just changed it all to hit.Parent instead of hit.Parent.Name. I also changed it to :GetPlayerFromCharacter

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:GetPlayerFromCharacter(hit.Parent)
		if plr then
			        plr("leaderstats"):FindFirstChild(currency).Value =  plr:FindFirstChild("leaderstats"):FindFirstChild(currency).Value + amt
			print("okay")
		end
		        elixir:Destroy()

	
      end
    end)

Do you know why it’s causing an error at if hit.Parent:FindFirstChild(“Humanoid”)?
I think it’s because it’s hitting some parts in the workspace before the player, but then why would it error if it’s just checking to see if the the parent is a Humanoid?

According to the error, to me it looks like currency doesn’t really exists. Because in the screenshot you provided, it had the folder

leaderstats
-Dark Elixir
-Elixir
-Gold

So you probably didn’t define the variable properly? Also, FindFirstChild(“needstobeinspeechmarks”)

Could be wrong and don’t understand what your defining tho, so lemme know.

Edit: Also use the getplayerfromcharacter as other people have stated.

If you don’t understand what I meant was get rid of local currency = “Elixir” and try and define the variable a diff way

1 Like

It shouldn’t do that, but what exactly is your error that you’re getting?

As for:

Maybe instead of that do something like:

if plr and plr.leaderstats:FindFirstChild(currency) then

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

end

Maybe it’s more complicated, so just do whatever suits you.

2 Likes

17:56:56.750 - Workspace.ElixirDrop.ElixirDropCollect:9: attempt to index nil with 'FindFirstChild'

Thanks it still comes up with the error but it’s working! Do you know why it works with your script and not mine?

No, I don’t know why mine works at all, but thanks anyways!
Also for your error:

After looking at some other topics about this, apparently hit.Parent doesn’t exist, so it’s nil.

Do you mind linking the topics. Thanks

Here:

and here:
https://devforum.roblox.com/t/music-script-attempting-to-index-nil-with-findfirstchild/661969

2 Likes

My thread is a different situation however

I know, but your error message is the same as she’s getting so it does help.