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?
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