Copy Scripts
Define p as something like script.Parent. Make another script that clones that script into all of the children of workspace.Frame3.
This isn't really a solution, but it does mean you won't have to change lots of scripts just because you want to try something different.
Functions
The code is the same thing for every p -- so we can just write a function.
function flash(p)
while true do
p.BrickColor = BrickColor.Red()
wait(0.1)
p.BrickColor = BrickColor.White()
wait(0.1)
p.BrickColor = BrickColor.Green()
wait(0.1)
end
end
then call that function for all of the children:
for _, child in pairs(workspace.Frame3:GetChildren()) do
flash(child)
end
This won't actually work, though. That's because the first while loop won't ever stop. We can spawn new coroutines to solve this:
for _, child in pairs(workspace.Frame3:GetChildren()) do
spawn(function() flash(child) end)
end
This will work (and actually works in a very similar way to just copying the scripts), but it isn't necessarily elegant.
One Loop
It depends on exactly what the pattern you want is. For instance, if you want them all to be the same color, you could do something like this:
function paint(bricks, color)
for _, brick in pairs(bricks) do
brick.BrickColor = color
end
end
while true do
paint( workspace.Frame3:GetChildren(), BrickColor.Red())
wait(0.1)
paint( workspace.Frame3:GetChildren(), BrickColor.White())
wait(0.1)
paint( workspace.Frame3:GetChildren(), BrickColor.Green())
end
But that will be boring.
You could get finer control, though:
function update(brick, data)
local name = brick.Name
local number = tonumber(name:match("%d+"))
local modulo = (data + number) % 3
if modulo == 0 then
brick.BrickColor = BrickColor.White()
elseif modulo == 1 then
brick.BrickColor = BrickColor.Green()
else
brick.BrickColor = BrickColor.Red()
end
end
function updateBricks(bricks, data)
for _, brick in pairs(bricks) do
update(brick, data)
end
end
while true do
for i = 1, 3 do
wait(0.1)
updateBricks( workspace.Frame3:GetChildren(), i)
end
end
You could do of course a lot more.
It comes down to some "update" function. It needs to figure out, "at this moment, for this brick, what color do I need to pick?" Then you just need to call update with whatever information it needs.
Here's another update function, as an example:
function update(brick)
local mag = brick.Position.magnitude
local amt = 0.5 + 0.5 * math.cos( mag / 20 + tick() )
brick.BrickColor = BrickColor.new( Color3.new(amt, amt, amt) )
end
1 comment
Thanks man