Attempt to Index Nil with 'Name' Error

Hi, I’m trying to make a game, but I ran into an error while doing so. In a folder (In workspace), I have 42 Models, with the name of Pair(And A Number). Each of these models have two parts. But, I made a script for whenever either of these parts are touched. When I try to print the parent’s (the model’s) number after the word “Pair”, it just says Attempt to Index Nil with ‘Name’ (Not the variable name). Look at the following screenshot:
partGood is the part in this case. If you can help, that would be amazing.
(also, partGood is basically model:GetChildren)

Try using “name = v.Parent.Name” instead of partGood. Also I’m a little confused why you need the for loop here?

1 Like

You are trying to get the parent of :GetChildren() which actually returns an array containing the children. If you want to get each child parent’s name, loop through them:

for _, instance in ipairs(partGood) do
    --code here

Continuing the discussion from Attempt to Index Nil with 'Name' Error:

I believe that you are trying to set up event listeners when each part in that part is touched.

For this problem, you have to understand the use of for loop with pairs

Says we have a folder inside our workspace.


when using this for loop,

for i , v in pairs(FOLDER:GetChildren()) do
     -- Do something

i represents the index, or to put it simply, a round of iteration that your for loop is in.
v represents the element that your for loop is working with.

Your error is on this line

local name = string.sub(partGood.Parent.Name, 4, 5)

if you want to display the parent’s name of v when v is touched.
Simple change it to,

local name = string.sub(v.Parent.Name, 4, 5)