Hi, so I'm making something that will look through whole workspace, including models, and if the parts/unions found is neon and has the Institutional white color, it will be changed to really red.
This is a small part of my script so you understand what's going on:
print("script ran") for i,v in pairs(game.Workspace:GetChildren()) do if v:IsA("Part") or v:IsA("UnionOperation") then if v.BrickColor == BrickColor.new("Institutional white") and v.Material == "Neon" then v.BrickColor = BrickColor.new("Really red") end elseif v:IsA("Model") then -- Then so on end end
This is my full script:
print("script ran") for i,v in pairs(game.Workspace:GetChildren()) do if v:IsA("Part") or v:IsA("UnionOperation") then if v.BrickColor == BrickColor.new("Institutional white") and v.Material == "Neon" then print("Found one") end elseif v:IsA("Model") then for i,v in pairs(v:GetChildren()) do if v:IsA("Part") or v:IsA("UnionOperation") then if v.BrickColor == BrickColor.new("Institutional white") and v.Material == "Neon" then print("Found one") end elseif v:IsA("Model") then if v:IsA("Part") or v:IsA("UnionOperation") then if v.BrickColor == BrickColor.new("Institutional white") and v.Material == "Neon" then print("Found one") end elseif v:IsA("Model") then if v:IsA("Part") or v:IsA("UnionOperation") then if v.BrickColor == BrickColor.new("Institutional white") and v.Material == "Neon" then print("Found one") end elseif v:IsA("Model") then if v:IsA("Part") or v:IsA("UnionOperation") then if v.BrickColor == BrickColor.new("Institutional white") and v.Material == "Neon" then print("Found one") end elseif v:IsA("Model") then if v:IsA("Part") or v:IsA("UnionOperation") then if v.BrickColor == BrickColor.new("Institutional white") and v.Material == "Neon" then print("Found one") end elseif v:IsA("Model") then print("Unscripted model") end end end end end end end end
Whenever you have the same bit of code over and over again, put it in a function instead (and anything that the bits of code differ on, add an argument to the function).
Since your code needs to reference itself within part of is algorithm (that is, you have to copy and paste it forever to make it work in all cases), you need recursion (a function calling itself).
Practically the same Scripting Helpers question
I hereby propose a recursive algorithm that is more flexible than theirs (but use whatever you feel comfortable with)
function Search(where, func) -- Search through 'where' recursively and apply function 'func' to each object found local ch = where:GetChildren() for i = 1, #ch do func(ch[i]) Search(ch[i]) end end local red = BrickColor.new("Really red") Search(workspace, function(obj) if obj:IsA("BasePart") and obj.BrickColor.Name == "Institutional white" and obj.Material == "Neon" then obj.BrickColor = red end end)
You really only need to use this particular Search
function if you intend to do multiple different recursive searches (ex maybe in the first you're looking for these neon parts and in a completely different location in the script you need to search for wooden parts). If it's only this one case that you need, it's easier to just use the one provided in the question linked above.