Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can't set a clone's parent to nil?

Asked by 6 years ago

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?

0
It's probably trying to do `nil.CoveredBy` which throws an error MooMooThalahlah 421 — 6y

1 answer

Log in to vote
-1
Answered by 6 years ago
Edited 6 years ago

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
0
Ahh that makes sense, thanks for the explanation and example! killajake25 0 — 6y
Ad

Answer this question