Attempt to index number with 'GetChildren'

Hi all. I am attempting to make a table containing a bunch of parts (instances). I initially wrote this code with the intention of making a string of the parts from sensors and their children. This worked fine. I then tried to rewrite the code (see below) so that it makes a table instead of a string, making changes where necessary. In doing so, I get the error (detailed below). Any advice?

Code:

local sensors = module.GetBlocks() -- module not shown, runs correctly
for i,v in pairs(sensors) do
	local c = v:GetChildren() -- error: "Attempt to index number with 'GetChildren'"
	for ind,val in pairs(c) do
		if val:IsA("Script") then
			table.remove(c,ind)
		else
			table.insert(sensors,ind)
		end
	end
end

Edit: sensors contains a table of 3 parts which all have a number of children (other parts).

The error contradicts with your statement as the error says there is a number value within the sensors table.

My initial guess is are you using table.pack()? This generates an additional parameter n in the table which is a number value which using pairs will loop through, using ipairs should solve this perhaps.

Have you also tried printing sensors? what is the output right before the error occurs?

When I print sensors, it returns the 3 parts in the table.

{
   [1] = BYDE,
   [2] = BYD1,
   [3] = BYD2
}

This is what I expected, as these are the 3 parts I get from sensors.

Odd, have you also printed v and the type of v as well?

for i,v in pairs(sensors) do
print(v, typeof(v) )
	local c = v:GetChildren() -- error: "Attempt to index number with 'GetChildren'"

Returns the 3 part names and “Instance”.

On second read I believe the error is caused in this line where you insert the index (number) into the sensors table.

Also rereading you initial objective

If I am correct do you want a table of all the parts within an instance? If so can you use :GetDescendants() instead with a :IsA(“BasePart”)

local sensors = module.GetBlocks() -- module not shown, runs correctly
local parts = {}
for i,v in pairs(sensors) do
	local c = v:GetDescendants()
for _, part in pairs(c) do
if part:IsA("BasePart") then
table.insert(parts, part)
end
end
end

Sorry wrote the code in rush to have formatting in the forum

1 Like

This solves it completely, thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.