Attempt to index nil with parent

Hey there Im working on a script and im getting the error ‘Attempt to index nil with parent’ here is my code

									   script.Parent.Parent.ChildAdded:connect(function(child)
											if child:IsA("Weld") and game.Players:GetPlayerFromCharacter(child.Part1.Parent)~=nil then
												local p=game.Players:GetPlayerFromCharacter(child.Part1.Parent)
												

The error is on line 2.

Its in a if statement so its quite strange.

Maybe there is some way to determine if the value is nil without throwing a error

Thanks in advance

What is child supposed to be?

A weld and the script wants to access its part1 instance

A property does not have a parent. (child.Part1.Parent)

1 Like

Its most likely that child.Part1 is nil

How would i be able to accesss the instance in that property?

If you are trying to check if child exists, try

game.Players:GetPlayerFromCharacter(child)~=nil

The property Part1 is always there if it’s a weld. So there is no need to check if the property exists.

Alright ill create a if incase it is nil

child.Part1 = -- whatever here

child.Part1 is nil. You need to make sure to validate it’s existence.

1 Like