I'm attempting to recreate chess and I have a function which adds an object value in every adjacent part for the king (Which I calculate from the part's name which has coordinate data)
However when the king is at the edge of the board my code checks for a part which doesn't exist, and so I use findFirstChild instead. I expected this to set the parent for the clone of my object value to nil, and thus not put it anywhere but instead it returns an error;
attempt to index a nil value
CODE:
clone = pieceValue:clone() clone.Parent = workspace:FindFirstChild(n - 1 .. l + 1).CoveredBy
What is a way of ignoring parts which don't exist or will I have to use an if statement for every part check I do?
It's doing what you'd expect and also not what you're expecting.
When it's trying to find the part in workspace it didn't find it so FindFirstChild returned nil but the reason you got the error is because you're indexing nil - nil.CoveredBy
local clone = nil local parent = nil clone = pieceValue:Clone() parent = workspace:FindFirstChild(n - 1 .. l + 1) if parent then clone.Parent = parent.CoveredBy else -- Do nothing since Parent is automatically set to nil end