Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How Can I Optimize This Script?

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.