Need to find models within a model, and then find lights within those models

  1. What do you want to achieve?
    So I have a model called “Train” which has 8 different sub-models called “Carriage1”, “Carriage2”, etc. until 8. Each of the carriages have multiple models within them, one of them which I need to access called “DoorLights”. “DoorLights” then has multiple lights attached to parts called “SurfaceLight”. I need to change the activity of the light after something happens, filtering to find only the lights called “SurfaceLight” within the model “DoorLights” in all carriages. I don’t want all the “SurfaceLight” throughout the train to be changed, only the ones within the “DoorLights” model

  2. What is the issue?
    The issue is that I know how to search for the lights within each model named DoorLights and change their properties, but I have to create a separate code for each carriage. I want to find a way where it first finds each model named “DoorLights” within the “Train” model, and then runs the search for “SurfaceLight” within “DoorLights”

  3. What solutions have you tried so far?
    I tried looking at the Dev hub for a situation like mine but struggled to find anything on it.

Any help would be appreciated! Lmk if you need code or anything from my project.

If all your lights have the same name, all you have to do is this.

for i, item in pairs(train:GetDescendants()) do
     if item.Name == “name of light here” then
          —do your code here
     end
end

And for context, :GetDescendants is a function that puts every single thing included within an item into a table

1 Like

Thanks for the response, let me just clarify what I need here. I don’t need every light to turn on, only the lights within the models called “DoorLights”. There is a “DoorLights” model in 8 different carriage models.

Here is my attempt at it:

for _, doorLights in ipairs(script.Parent.Parent.Parent.Parent.Parent:GetChildren()) do 
--that long identifier is the "Train" model
	if (doorLights.Name == "DoorLights") then
		for _, doorLightsLights in ipairs(--[[I dont know what goes here and how to get it]]) do
			if (doorLightsLights.Name == "SurfaceLight") then
				doorLightsLights.Active = true
			end
		end
	end
end

He says he only wants to find surfaceLights within a certain model. Instead of searching the whole train, he should be using this:

train = workspace.Train

for _,v in pairs(train:GetDescendants()) do
    if v.Parent.Parent.Name == "DoorLights" and v.Name == "SurfaceLight" then
        --do stuff
    end
end

GetDescendants will collect every single instance inside of an instance. every single model,sound,etc in your entire train model. You know that the surfaceLight is inside of a part, inside of a model called DoorLights. Hence, v (the specific instance being indexed at that time) .Parent (the part the light is inside of) .Parent (the model that part is inside of aka “DoorLights”) .Name

1 Like

Brilliant, this is exactly what I needed. Thanks a lot!