Index nil with :WaitForChild()

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Make a collectable money system
  2. What is the issue? Include screenshots / videos if possible!
    There is an error that says “Attempt to index nil with :WaitForChild()”
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? None of those matched my problem.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

   script.Parent.Touched:Connect(function(hit)
	local playerChar = hit.Parent
	local inPlayers = game.Players:GetPlayerFromCharacter(playerChar)
	local leaderstats = inPlayers:WaitForChild("leaderstats") --The part where the error occurs
	local robux = leaderstats:WaitForChild("Robux")
	robux.Value = robux.Value + 25
	script.Parent:Destroy()
end)





Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

This error means WaitForChild() failed because what you’re waiting for does not exist.

Have you created a leaderstats folder inside the player instances? Have you created the leaderstat value for “Robux”?

Yes, I did, I have a leaderstats script in my game.

This may help you get your leaderstats setup.

Could you share this code? It may assist someone in helping you.

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local robux = Instance.new("IntValue")
	robux.Name = "Robux"
	robux.Parent = folder
	robux.Value = 0
end)

Print playerChar and inPlayers just to make sure that they are correct.

2 Likes

Give this a go:

script.Parent.Touched:Connect(function(hit)
  local playerChar = hit.Parent
  local inPlayers = game.Players:GetPlayerFromCharacter(playerChar)
  if not inPlayers or inPlayers.Parent ~= game.Players then return end
  local leaderstats = inPlayers:WaitForChild("leaderstats") 
  local robux = leaderstats:WaitForChild("Robux")
  robux.Value = robux.Value + 25
  script.Parent:Destroy()
end)
1 Like

any part.touched events will fire if they touch anything (ie not just player characters), so if you try to GetPlayerFromCharacter(hit.Parent), unless there is a player called Baseplate or Part, it’s likely to return nil. :slight_smile:

It’s best to look for the humanoid first to confirm it is actually a player character that touched the part.

script.Parent.Touched:Connect(function(hit)
	local playerChar = hit.Parent
        if playerChar:FindFirstChildWhichIsA("Humanoid") then
	       local inPlayers = game.Players:GetPlayerFromCharacter(playerChar)
	       local leaderstats = inPlayers:WaitForChild("leaderstats")
	       local robux = leaderstats:WaitForChild("Robux")
	       robux.Value = robux.Value + 25
	       script.Parent:Destroy()
        end
end)

(lol, post hadn’t updated, nvm!)

2 Likes

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