DarkMenacing
(Developer_WhoCreates)
October 16, 2022, 4:57pm
#1
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
DasKairo
(Cairo)
October 16, 2022, 5:01pm
#2
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
DarkMenacing
(Developer_WhoCreates)
October 16, 2022, 5:02pm
#4
But, what if the parts’s parent were from a model like a door?
DasKairo
(Cairo)
October 16, 2022, 5:03pm
#5
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
DasKairo
(Cairo)
October 16, 2022, 5:04pm
#7
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
DarkMenacing
(Developer_WhoCreates)
October 16, 2022, 5:05pm
#8
But im using a localscript
[max char]
DasKairo
(Cairo)
October 16, 2022, 5:06pm
#9
either way, you solved it, doesnt matter