Attempt to index nil with "Head"

Hello!

I keep getting this error:

image_2024-03-04_071538508

This is the section of the script which the error keeps coming from:

players.PlayerAdded:Connect(function(plr)
	local titleFolder = plr:WaitForChild("TitleFolder")
	local titleStat = titleFolder:WaitForChild("TitleStat")
	local char = plr.Character
	
	local overheadFrame = char.Head:WaitForChild("billboard2").Frame -- error here

Why is it not working? Any help is appreciated, thanks!


EDIT

Thanks for your help everyone! Before coming back to check this post, I’ve managed to find a fix which was adding plr.CharacterAdded.

players.PlayerAdded:Connect(function(plr)
	local titleFolder = plr:WaitForChild("TitleFolder")
	local titleStat = titleFolder:WaitForChild("TitleStat")
	
	plr.CharacterAdded:Connect(function(char)
		task.wait()
		
		local overheadFrame = char.Head:WaitForChild("billboard2").Frame

Thank you all for your help!

Maybe use

local char = Player.Character or Player.CharacterAdded:Wait()
3 Likes

Try this;

game.Players.PlayerAdded:Connect(function(plr)
	local titleFolder = plr:WaitForChild("TitleFolder",30)
	local titleStat = titleFolder:WaitForChild("TitleStat",30)
	local char = game.Workspace:WaitForChild(plr.Name,30)
	local overheadFrame = char:WaitForChild("Head",30):WaitForChild("billboard2",30).Frame -- error here
1 Like

What @SomeFedoraGuy said should work to solve your problem, but I’ll explain why the error is happening in the first place. Even though your script is a server script (I concluded this due to the fact the error shows that it’s inside of ServerScriptService) and Instances are usually ready to use on the server-side (without the need to use WaitForChild), there are still cases where the server will fail to retrieve an Instance, which are:

  • If the Instance hasn’t been created either by a separate server script or by the game engine in time before it is indexed (Which is what’s happening in your case)
  • If the Instance was created on the client-side, usually using a LocalScript
  • If the Instance was destroyed before it is indexed

PlayerAdded is fired when a player joins the game, after a Player Instance is created for them. The player’s character takes a bit more time to finish loading, which is why you’ll still need to wait for it on the server by using a solution like the one SomeFedoraGuy provided. Another possible solution would be to place the server script inside of StarterCharacterScripts, which would start to run after the character has finished loading, but this method would depend on what the script is trying to do as it will also automatically run after the character dies or resets

2 Likes

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