Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I get all instances of certain class?

I am trying to get all the PointLights in a model. They are embedded in model in a model in a model, etc.

Here is my current code. I want lights array to be all the PointLights. named PointLight.

local lights = {}
local children = house:GetChildren()
for i = 1, #children do
    if children[i].Name == "PointLight" then
		lights.append(children[i])
	end
end

On a side note is .append() a real thing in LUA? (I have not tested it because I cannot get there)

children is contains just the direct descendants of house

2 answers

✓ Accepted answer
local thing = game.Workspace --place to be searched
for i,things in pairs(thing:GetDescendants()) do -- or :GetChildren()
if things:IsA("PointLight") then
things:Destroy() -- JUST A CODE
end
end

Just loop through the children in the model and check if they inherit the "PointLight" class, like so.

local lights = {}

for i, child in pairs(house:GetChildren()) do
	if child:IsA("PointLight") then
		table.insert(light, child) -- insert `child` into the table `light`.
	end
end

2 comments

0

-1 for ineffiency

0

@cabbler do you have a more efficient solution?

This discussion is preserved as it was. New answers, comments, and votes are closed.