How to make random lights turn on in a random order

  1. What do you want to achieve?
    Hello ! I want to turn on lights one by one in a random order. (Without selecting the lights one by one like : local light 1, local light 2, local light 3)
    I put a picture to demonstrate what do i want to do.

  2. What is the issue?
    I don’t know how to do it without selecting the lights one by one.

  3. What solutions have you tried so far?
    I looked on the devforum and others and i found nothing.

1 Like
local parent

for v, part in pairs(parent:GetChildren()) do
    print("Light",v,"its",part)
end

Here try this:

local lights = {} -- put the light instances in this table.

local delay = 1

while true do

local randomLight = lights[math.random(1, #lights)]

randomLight.Enabled = true

task.wait(delay)

randomLight.Enabled = false

end
local lights = game.Workspace.Lights:GetChildren()
while true do
    for i, v in pairs(lights) do
        lights.Light.Enabled = false
   end
    lights[math.Random(1,#lights).Light.Enabled = true
    wait(1)
end

this will work assuming all the lights are in a group called "Lights’ located in the workspace. Also change whatever light part you have within them to be called “Light”

Thank you for your reply but there is an error at line 6 how to i fix it ?

replace

  lights[math.Random(1,#lights).Light.Enabled = true

with

  lights[math.Random(1,#lights)].Light.Enabled = true

Ok and if i want to change the parts material how do i do it ?

Do .Material = Enum.Material.[Put your material after the dot] eg: Enum.Material.Cobblestone

local lights = game.Workspace.Lights:GetChildren()
local originalMaterial = lights[1].Material
local newMaterial = Enum.Material.*whatever it is*

while true do
    for i, v in pairs(lights) do
        v.Light.Enabled = false
v.Light.Material = originalMaterial
   end
    local slights = lights[math.random(1,#lights)]
slights.Light.Enabled = true
slights.Material = newMaterial

    wait(1)
end

I updated my script, try it now

I get this
image

This is how my testing model is organized
image

(I didn’t put the light for the moment i just put the part to change the material)

Put the lights in, and rename them to “Light”, otherwise it will error

Okay, i managed to make the script work.
But now the lights turn on and off randomly
How do i make it so when all the lights are turn on the script stop ?

local lights = game.Workspace.Lights:GetChildren()
local originalMaterial = lights[1].Material
local newMaterial = Enum.Material.*whatever it is*
local nLights = lights

for i = 1, #lights do
   local chosen = nLights[math.random(1, #nlights)]
   chosen.Material = newMaterial
   chosen.Light.Enabled = true
   table.remove(nLights, table.find(nLights, chosen))
  wait(1)
end

Ok it worked thank you for helping me !