Attempt to index nil with 'FindFirstChild'

I have a game with an ability system, and for this specific tool, it shall remove the cooldown and allow them to spam a projectile. The game gives me an error that says “Workspace.projectileball.ProjectileScript:4: attempt to index nil with ‘FindFirstChild’” whenever there is alot of projectiles. I’m not sure what the problem is.

local projectile = script.Parent

projectile.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
if hit.Parent.Name ~= script:GetAttribute(“Immune”) then
hit.Parent.Humanoid:TakeDamage(10)

  	local playerName = script:GetAttribute("Immune")
  	
  	local player = game.Players[playerName]
  	
  	player.leaderstats.Coins.Value += 1

  	projectile:Destroy()
  end

end
end)

Try doing instead of FindFirstChild() WaitForChild() maybe it’s gonna solve the problem?

doesnt seem to fix the problem. it now says “Workspace.projectileball.ProjectileScript:4: attempt to index nil with ‘WaitForChild’”

That bug occurs whenever you’re trying to call :FindFirstChild on an instance that doesn’t exist, in this case, that’d be hit.Parent. My best guess for why hit.Parent is coming back as nil is that whoever the network owner of the object is, is saying a nonexistent (deleted on the server but not yet on the client) was touched.
Making sure hit.Parent ~= nil should fix the problem.

if hit.Parent and hit.Parent:FindFirstChild(“Humanoid”) then
2 Likes

Thanks, i got the issue fixed.

1 Like

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