Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How Can I Optimize This Script?

Asked by 6 years ago
Edited 6 years ago

I have a large number of parts that I want to turn blue when touched. Unfortunately, I need to insert the same script into every single part, which is very tedious.

Here is the script:

script.parent.Touched:connect(function(plr)
    script.parent.BrickColor = BrickColor.Blue()
end)

Is there any way I can cycle through all the parts and run this script instead of having to insert it into every single part?

1 answer

Log in to vote
0
Answered by
Tomstah 401 Moderation Voter
6 years ago

You'll want to use a generic for loop and you'll also want to parent all your touchables to one thing, whether it's a model or a folder it doesn't really matter, but it'll look like this:

for _, Child in ipairs(CommonParent:GetChildren()) do
    Child.Touched:Connect(function(ActedPart)
        Child.BrickColor = BrickColor.Blue()
    end)
end
Ad

Answer this question