How would i check if a folder has model(s) called "Head Lights" and then make the parts neon?

this is what i have so far:

LightButton.ClickDetector.MouseClick:Connect(function()
	if HasBat.Value == true then
		for _,v in script.Parent.Parent do
			if v.Name == "Headlights" then
					print("turning on light(s)!")
			end
		end
	end
end)

Im not sure how to use i,v loops, but i fell like its the right thing to use… The car can have multiple headlights, from 0-2 and i want to make sure that im turning only the existing ones on, and doing nothing if there are no lights

Perhaps something like this, find the folder, and get the parts in it, if those are parts turn them into neon:

LightButton.ClickDetector.MouseClick:Connect(function()
	if HasBat.Value == true then
		local theFolder = script.Parent.Parent:FindFirstChild("Headlights")
		
		if theFolder then
			for _,v in pairs(theFolder:GetChildren()) do
				if v:IsA("BasePart") then
					print("turning on light(s)!")
					v.Material = Enum.Material.Neon
				end
			end
		end
	end
end)

will this work with models?
for example, i want to look for model(s) called “Headlights” and then if it exists, go to the child of the model called “Light” then make it neon

Ok, let me change the script. You just need to check if its a model, if its a model you iterate that model using the for loop and getChildren

LightButton.ClickDetector.MouseClick:Connect(function()
	if HasBat.Value == true then
		local theFolder = script.Parent.Parent:FindFirstChild("THE FOLDER WITH THE MODELS")
		
		if theFolder then
			for _, model in pairs(theFolder:GetChildren()) do
				if model:IsA("Model") then
					warn("found a model")
					
					for _, partInModel in pairs(model:GetChildren()) do
						print("turning part to neon", partInModel)
						partInModel.Material = Enum.Material.Neon
					end
				end
			end
		end
	end
end)
1 Like

I think it can be more practical if you rename your headlights with the same name for both, and you GetDescendants() (of your vehicle). When you find the part/mesh part with the correct name, you change it to neon. Something like this :

LightButton.ClickDetector.MouseClick:Connect(function()
	if HasBat.Value == true then
		for _,part in ipairs(script.Parent.Parent:GetDescendants()) do
			if part.Name == "Headlights" then -- Change your 2 headlight's name to the same name
					print("turning on light(s)!")
                    -- Your code to actually turn them to neon
			end
		end
	end
end)

You could also create an attribute or tag in the headlights part/mesh like this (in the properties tab) :
image
If you use an attribute, you will still have to get the descendants of the vehicle.
But if you use tags as you can see in the image, you can get the part way easier (CollectionService | Documentation - Roblox Creator Hub)

why is there a “if thefolder” what does that do?

uhhhh it transported the car to heaven…

It only checks if the instance that contains the models you did mention exist or not, in order to not try to iterate something that doesnt exist.

And as @1_Lukeskyiwalker suggested, you could find more practical ways to achieve it. I do not think that way is better, but using Tags would be, to find the specific parts you want to change by using its Tag, instead of names

1 Like

lemme explain it more in depth,
the folder that im looking through contains ALL car parts, the engine, the frame, the lights, ect
i want to look for models called “headlights” in that folder, and then turn the light part neon.
the headlights have 2 light parts, “Light” and “Turn signal” i want only “Light” to become neon

Hopefully thats a good explanation. Thank!

Oh also to add, the light part also have some parts that are not baseparts, like a script and click detector

LightButton.ClickDetector.MouseClick:Connect(function()
	if HasBat.Value == true then
		for _, model in pairs(script.Parent.Parent:GetDescendants()) do
			if model:IsA("Model") and model.Name == "headlights" then
				warn("found another model called headlights")

				for _, partInModel in pairs(model:GetDescendants()) do
					print("turning part to neon", partInModel)
					partInModel.Material = Enum.Material.Neon
				end
			end
		end
	end
end)

Iterate all stuff inside script.Parent.Parent, which as you said is the container of the whole car…
Get all stuff inside the car, check if any is a model and its name is “headlights”…
If a model called headlights its in there, perform a second loop to iterate the parts inside that model, if any is a part turn them to neon.

Would be more efficient to get parts tagged as headlights inside the car model, instead of iterating whole car just to find the lights…

just tried it, it kinda works but it turns on the “Turn signal” as well.
It also is trying to set the material of a script and weld.
How do i fix this?

I forgot to add the check if its a base part, there:

LightButton.ClickDetector.MouseClick:Connect(function()
	if HasBat.Value == true then
		for _, model in pairs(script.Parent.Parent:GetDescendants()) do
			if model:IsA("Model") and model.Name == "headlights" then
				warn("found another model called headlights")

				for _, partInModel in pairs(model:GetDescendants()) do
					if partInModel:IsA("BasePart") then
						print("turning part to neon", partInModel)
						partInModel.Material = Enum.Material.Neon
					end
				end
			end
		end
	end
end)

Still, think about using tags:

It also is trying to set the material of a script and weld.

above

if not partInModel:IsA("BasePart") then continue end
partInModel.Material = Enum.Material.Neon

now it wont turn on at all
char limit

show output, you should see a print per part found:
print("turning part to neon", partInModel)

i look at the output yea, nothing was printed

If the previous script I gave you was working, at least this print should happen:
warn("found another model called headlights")

LightButton.ClickDetector.MouseClick:Connect(function()
	if HasBat.Value == true then
		for _,v in script.Parent.Parent:GetDescendants() do
			if v.Name == "Light" and v:IsA("BasePart") then
				print("turning on light(s)!")
			end
		end
	end
end)

This would look for any part name “Light” in your model and will do whatever you want.

it worked after fixing Case sensitive stuff, but it still turns on every light, not just the one i want

I tested the script works normally for me, check what you are doing:

LightButton.ClickDetector.MouseClick:Connect(function()
	if HasBat.Value == true then
		for _, model in pairs(script.Parent.Parent:GetDescendants()) do
			if model:IsA("Model") and model.Name == "headlights" then
				warn("found another model called headlights")

				for _, partInModel in pairs(model:GetDescendants()) do
					if partInModel:IsA("BasePart") then
						print("turning part to neon", partInModel)
						partInModel.Material = Enum.Material.Neon
					end
				end
			end
		end
	end
end)

Captura