For loop patterns

How would I make patterns with for loops? Like lights, but they enable in a spiral or lights that enable in other patterns

Can you go into a little more detail about what your ultimate goal is?

ill make something in paint.net please wait < not that too hard to explain

I mean like, you have lights, and a for loop, you want to enable the lights in patterns using the for loop
like a spiral or like row per row

I suppose you would need to use 2-Dimensional Arrays to compute something like that and assign light piece to each t[Row][Column]:

local Array = {}

local LightPieces = Model:GetChildren()

local Rows = 2
local Columns  = #LightPieces

for Row = 1, Rows do
    Array[Row] = {}
    for Column = 1, Columns do
        Array[Row][Column] = LightPieces[Column]
    end
end

To access each light piece, you would then just do Array[1][3].Material = Enum.Material.Neon
(In Row 1, Column 3 set the light piece to Neon)

Well, you could do things procedurally or you could do things frame-by-frame.

Procedurally (i.e. configurable patterns based on functions) is sort of easy. Let’s say your lights are a bunch of PointLights:

local pointLights = workspace.Lights:GetChildren()

Define some 3D function that varies with time, like a ripple effect:

local function Ripple(x, z, t)
	local circ = x*x + z*z
	return math.cos(circ * circ / (1 + circ) - t)
end

I made a desmos graph that visualizes this. Press the play button on the left side next to t_0:

Then for each light, call that function. If it’s > 0, turn the light on. If it’s < 0, turn the light off:

for _, light in lights do
  local pos

  if light.Parent:IsA("BasePart") then pos = light.Parent.Position
  elseif light.Parent:IsA("Attachment") then pos = light.Parent.WorldPosition
  else continue end -- skip

  local shouldBeOn = Ripple(pos.X, pos.Z, tick())
  light.Enabled = shoudBeOn
end

Do that every frame, and ya done. Plug in different functions for Ripple for different effects.


That was procedural. Frame-by-frame would just be defining a list of states for your lights and looping through them. It would probably be a little bit of a pain to set up, but you could make the workflow easier with a basic plugin or something.

You could also make it a bit more general, and have like grid-based keyframe animations and then assign each light to a grid space.