Attempt to index nil with WaitForChild

So I’m trying to make it where parts spawn every couple seconds, and whenever you touch them, the Parts stats goes up by one. Instead, I get an error saying:
Workspace.Part.Script:7: attempt to index nil with ‘WaitForChild’
Here is my server script:

local Players = game:GetService("Players")

local part = game.ServerStorage:WaitForChild("Part")

Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local PartsCollected = Instance.new("IntValue")
	PartsCollected.Value = 0
	PartsCollected.Name = "Parts"
	PartsCollected.Parent = leaderstats
end)

while true do
	task.wait(2)
	local newPart = part:Clone()
	newPart.Parent = workspace
end

Here is the script inside the part:

local part = script.Parent

part.BrickColor = BrickColor.random()

part.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local leaderstats = Player:WaitForChild("leaderstats")
	
	Player.leaderstats.PartsCollected.Value += 1
	part:Destroy()
end)

If you think you know what went wrong, please let me know. Thank you and have a wonderful day! :slight_smile:

You can try any of these options:

local part = script.Parent

part.BrickColor = BrickColor.random()

part.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player then
          local leaderstats = Player:FindFirstChild("leaderstats")
	
	      Player.leaderstats.PartsCollected.Value += 1
	      part:Destroy()
       else
           print("Something is wrong...") 
    end
end)
local part = script.Parent

part.BrickColor = BrickColor.random()

part.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local leaderstats = Player:WaitForChild("leaderstats")
	
	leaderstats.PartsCollected.Value += 1
	part:Destroy()
end)
local part = game.ServerStorage:WaitForChild("Part")

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local PartsCollected = Instance.new("IntValue")
	PartsCollected.Value = 0
	PartsCollected.Name = "Parts"
	PartsCollected.Parent = leaderstats
end)

while true do
	task.wait(2)
	local newPart = part:Clone()
	newPart.Parent = workspace
end

Sadly, none of those work. The first one just keeps spamming “Something is wrong” and number 2 prints the same error message. Number 3 seems to work fine, though.

1 Like

Can you print out hit.Parent and let us know what the output says? I’m assuming the parent of the hit variable is not the character and therefore the GetPlayerFromCharacter() function returns nil

The issue is that your script assumes that the hit is one of the character body parts that isn’t always the case, if the hit is another random part or a handle within a tool/accessory of the character hit.Parent won’t be their character and thus the game.Players:GetPlayerFromCharacter(char) function will return nil. To fix this, you simply have to check if it returns nil, and if it does ignore it:

local part = script.Parent

part.BrickColor = BrickColor.random()

part.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not Player then return end --if player is nil, don't run the code below
	Player.leaderstats.Parts.Value += 1
	part:Destroy()
end)

That works, but now I get this error:
image

Replace PartsCollected with Parts since that’s the naming you use in your leaderstats script:

Player.leaderstats.Parts.Value += 1 --Parts not PartsCollected!

It works! Thank you so much! Have a good day!

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