How do i change multiple parts material instantly?

Hi, my name is DarkMenacing.

I was making a script for my game and i wanted to make a script that change multiple parts material, so i made one

script.Parent.MouseButton1Click:Connect(function()
	workspace.Folder1.Part1.Material = Enum.Material.Metal
	workspace.Folder1.Part2.Material = Enum.Material.Metal
	workspace.Folder1.Part3.Material = Enum.Material.Metal
	workspace.Folder1.Part4.Material = Enum.Material.Metal
	workspace.Folder2.Part1.Material = Enum.Material.Metal
	workspace.Folder2.Part2.Material = Enum.Material.Metal
	workspace.Folder2.Part3.Material =Enum.Material.Metal
	workspace.Folder2.Part4.Material = Enum.Material.Metal
end)

But then i realized that the script would be way too long if i added more parts at my game, so how do make a shorter script that functions the same as the one up?

Anything will help! - Darkmenacing

1 Like
for _,i in pairs(script.Parent:GetChildren()) do
if i:IsA("Part") then
i.Material = Enum.Material.Metal
end
end
1 Like

you can use a for loop to change the material of every object in the folder

for i,v in next, FolderHere:GetChildren() do
if v:IsA("BasePart") then
v.Material = Enum.Material.Metal
end
end

But, what if the parts’s parent were from a model like a door?

you path the for loop to the door

it doesn’t matter what object the parts are in, just replace folderhere with the object you want to loop through

1 Like
for _,i in pairs(script.Parent:GetChildren()) do -- Gets the children of the pathed Instance
if i:IsA("Part") then -- Check if its a part
i.Material = Enum.Material.Metal -- Changes Material of the parts
end
end

if they dont work, put them into ServerScriptService and path the for loop to it

But im using a localscript
[max char]

either way, you solved it, doesnt matter