Attempt to index nil with 'Parent'

I am trying to create a Chess Engine and I want to make a Pawn move forward by pressing “W”. However, it does not work and I get the error shown in the title. For more info, pressing “WASD” can change where the pawn moves, but it doesn’t secure the position, pressing space will secure the position (so can’t be moved) I haven’t implemented ASD and space because W doesn’t work.

function Pawn:Move(currentPos, validPosTable)
	for i, v in pairs(validPosTable) do
		validPosTable[i] = Engine:GetCoord(v)
	end
	
	UserInputService.InputBegan:Connect(function(inputObject, gameProcessed)
		if inputObject.KeyCode == Enum.KeyCode.W then
			for i, v in pairs(validPosTable) do
				if Engine:GetCoord(currentPos)[1] == v[1] and v[2] - Engine:GetCoord(currentPos)[1] == 1 then
					local nextCoord = {Engine:GetCoord(currentPos)[1], v[2]}
					local nextAlgebraic = Engine:GetAlgebraic(nextCoord)
					local pawnPiece = Chessboard[currentPos]:FindFirstChildWhichIsA("ImageLabel")
					pawnPiece.Parent = Chessboard[nextAlgebraic] --error occurs
				end                                                                 
			end
		end
	end)
end
for _, frame in pairs(script.Parent.Chessboard.Frame.Board:GetChildren()) do
	for _, button in pairs(frame:GetChildren()) do
		if button:IsA("TextButton") then
			button.Activated:Connect(function()
				
				--reset square to default color
				for i, v in pairs(defaultColors) do
					Chessboard[i].BackgroundColor3 = v
				end
				
				--check to make sure pawn is in square
				if #button.Parent:GetChildren() > 1 and string.match(button.Parent:FindFirstChildOfClass("ImageLabel").Name, "wPawn_") then
					frame.BackgroundColor3 = Color3.fromRGB(150, 189, 147)
					PawnEngine:Move(button.Name, PawnEngine:GetValidMoves(button.Name))
					for _, v in pairs(PawnEngine:GetValidMoves(button.Name)) do
						Chessboard[v].BackgroundColor3 = Color3.fromRGB(202, 255, 198)
					end
				end	
				--continue to move image
				button.Parent:FindFirstChildOfClass("ImageLabel").AncestryChanged:Connect(function(_, parent)
					PawnEngine:MoveTo(parent.Name, PawnEngine:GetValidMoves(parent.Name))
				end)
			end)
		end
	end
end

This might seem confusing but here is what the code is trying to do:

Chessboard
|
|__ Square tile (A1, B2, C3 chess coord etc.)
|   |
|   |__ Button ( for selecting)
|   |
|   |__ Image1 ( actually displays the pawn)
|
|__ Another Square tile
    |
    |__ Image1 does not move here it should

I know its confusing because I have like 10 other scripts not here but if anyone needs more clarification, please ask!

Are you able to share the output bar and tell me what line it is in?

Its in the code block …

It states it in the error. pawnPiece is nil.

Are you sure there is an imageLabel inside the piece object?

If there is, try FindFirstChildOfClass? I use this a lot more but not sure what the difference really is.

2 Likes

To add on to what @bryancololee said, it could also be deeper in the hierarchy. For instance, this will give me an error if the part has a click detector.

local blah = game.Workspace:FindFirstChildWhichIsA("ClickDetector") --> only captures the immediate children of Workspace
print(blah) --> prints nil since the click detector is inside of a part in the workspace
blah.Parent = game.Workspace --> errors saying attempt to index nil with 'Parent'

If this is your case, then you can search recursively (checking all the descendants) by adding a true to your check, like this:

local pawnPiece = Chessboard[currentPos]:FindFirstChildWhichIsA("ImageLabel", true)

Note that this will find the very first child that’s an image label, so if you have multiple image labels inside Chessboard[currentPos], then you could capture the wrong one.