How do I fix attempt to index nil with 'FindFirstChild'

Hello, I have this problem with the error. Idk why it says that because its a variable.

20:03:11.873  Workspace.OzeanSyler.HealthbarServerSetup:16: attempt to index nil with 'FindFirstChild'  -  Server - HealthbarServerSetup:16
  20:03:11.873  Stack Begin  -  Studio
  20:03:11.873  Script 'Workspace.OzeanSyler.HealthbarServerSetup', Line 16  -  Studio - HealthbarServerSetup:16
  20:03:11.873  Stack End  -  Studio

and this is the script until line 16:





if script.Parent.Parent == "NowPlates" then
	script:Destroy()
end

local Players = game:GetService("Players")
local Char = script.Parent
local Plr = Players:GetPlayerFromCharacter(Char)
local Humanoid = Char:FindFirstChild("Humanoid")
local Head = Char:FindFirstChild("Head")

local Healthbar = script:FindFirstChild("HealthBarGui")
local Container = Healthbar:FindFirstChild("HealthBarContainer")

does anyone know how to fix it?
any help is appreciated!

3 Likes

Use :WaitForChild() instead of FindFirstChild().
My assumption here would be that the FindFirstChild() returned nil because your object didn’t have time to load yet. WaitForChild() will wait (indefinitely) until your object is added.
Please let me know if this worked !

1 Like

that doesnt work either, now it says attemp to index nil with waitforchild

What is this script parented to?

Did you change all of these :

local Humanoid = Char:FindFirstChild("Humanoid")
local Head = Char:FindFirstChild("Head")

local Healthbar = script:FindFirstChild("HealthBarGui")
local Container = Healthbar:FindFirstChild("HealthBarContainer")

Or only one ? I would recommend you change all of these to WaitForChild()

its parented to the character ekjbjdhfb

How are you sure? Because clearly the character is nil.
Or it could be the container, where is it at also?

it was waitforchild before and i changed it to findfirstchild because waitforchild didnt work too

the script is in startercharacterscripts and the container is parented to the child of the script