Attempt to index nil with 'Parent'

Hello, What is wrong with my script?

local Name = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Name

while true do
	wait()
	if game.Players[Name.Name].PLR_LRC_MDT.CAD_Call.Value == nil then

	script.Parent.Status.StatusText.Text = game.Players[Name.Name].PLR_LRC_MDT.Status.Value
	script.Parent.Callsign.Text = game.Players[Name.Name].PLR_LRC_MDT.Callsign.Value
	script.Parent.Call_Status.StatusText.Text = game.Players[Name.Name].PLR_LRC_MDT.CAD_Call.Value.Name
		
	else
		
		script.Parent.Status.StatusText.Text = game.Players[Name.Name].PLR_LRC_MDT.Status.Value
		script.Parent.Callsign.Text = game.Players[Name.Name].PLR_LRC_MDT.Callsign.Value
		script.Parent.Call_Status.StatusText.Text = "NO CALL"
	end
end

Error:

Players.arestformoney.PlayerGui.LRC_MDT.Main_Frame.Control.Unit_Call_Overview.Control_UNIT_Handler.Unit_Templete.Unit_Info:1: attempt to index nil with 'Parent'

This is a ServerScript BTW

Two possibilities:

  1. Something is setting the script variable to nil, causing the error to occur.

  2. You added more parents than the ones needed, climbing up the instance tree and reaching the parent game, that has a parent of nil, causing the error. Basically you did game.Parent.Parent which translates to nil.Parent that causes that error.

1 Like

If the error is from this line of code then man there’s so much parent. Try defining a better path. you may have added one in extra.

Adding onto what @NyrionDev and @MilkyFiend said, if the parent directory is intended to lead to a Player, you can just do:

local localPlayer = script:FindFirstAncestorOfClass("Player")

If a Player is found as an ancestor of the script, it will be returned. Otherwise, it will return nil.