Blink light loop ∞

Hey, i want to blink all light in a folder in exact like that without tween like between but that blink 1 light per 1 light, how i can do ?

while true do
for _,v in pairs(script.Parent:GetDescendants()) do
        if v.ClassName == "PointLight" or v.ClassName == "SpotLight" or v.ClassName == "SurfaceLight" then
            print("a")
            wait(0.1)
            v.Brightness = 0.8

            wait(0.1)
            v.Brightness = 0.5

            wait(0.1)
            v.Brightness = 0.8

            wait(0.1)
            v.Brightness = 0.5

            wait(0.1)
            v.Brightness = 0.8

            wait(0.1)
            v.Brightness = 1

            wait(0.1)
            v.Brightness = 0.8

            wait(0.1)
            v.Brightness = 0.5

            wait(0.1)
            v.Brightness = 1

            wait(0.1)
            v.Brightness = 0.8

            wait(0.1)
            v.Brightness = 0.5

            wait(0.1)
            v.Brightness = 0.8

            wait(0.1)
            v.Brightness = 0.1
        end
    end
end

To achieve the desired effect of blinking each light in the folder one after another, you need to modify the loop.

Try this:

local lights = {}

for _,v in pairs(script.Parent:GetDescendants()) do
  if v.ClassName == "PointLight" or v.ClassName == "SpotLight" or v.ClassName == "SurfaceLight" then
    table.insert(lights, v)
  end
end

while true do
  for i = 1, #lights do
    lights[i].Brightness = 0.8
    wait(0.1)
    lights[i].Brightness = 0.5
    wait(0.1)
    lights[i].Brightness = 0.8
    wait(0.1)
    lights[i].Brightness = 0.5
    wait(0.1)
    lights[i].Brightness = 0.8
    wait(0.1)
    lights[i].Brightness = 1
    wait(0.1)
    lights[i].Brightness = 0.8
    wait(0.1)
    lights[i].Brightness = 0.5
    wait(0.1)
    lights[i].Brightness = 1
    wait(0.1)
    lights[i].Brightness = 0.8
    wait(0.1)
    lights[i].Brightness = 0.5
    wait(0.1)
    lights[i].Brightness = 0.8
    wait(0.1)
    lights[i].Brightness = 0.1
    wait(0.1)
  end
end

that execute for 1 time to a PointLight and start again but not all lights :confused: (1 per 1 light)

You can modify the code to loop through each light, blink it once, and then move on to the next light:

while true do
  for _, v in pairs(script.Parent:GetDescendants()) do
    if v.ClassName == "PointLight" or v.ClassName == "SpotLight" or v.ClassName == "SurfaceLight" then
      v.Brightness = 0.8
      wait(0.1)
      v.Brightness = 0.5
      wait(0.1)
      v.Brightness = 0.8
      wait(0.1)
      v.Brightness = 0.5
      wait(0.1)
      v.Brightness = 0.8
      wait(0.1)
      v.Brightness = 1
      wait(0.1)
      v.Brightness = 0.8
      wait(0.1)
      v.Brightness = 0.5
      wait(0.1)
      v.Brightness = 1
      wait(0.1)
      v.Brightness = 0.8
      wait(0.1)
      v.Brightness = 0.5
      wait(0.1)
      v.Brightness = 0.8
      wait(0.1)
      v.Brightness = 0.1
    end
  end
end

I mean all lights need to blink at same time that the prob here.

Here’s an example to blink all lights at the same time:

local function blink(light)
	for i = 1, 3 do
		light.Brightness = 0.5
		wait(0.1)
		light.Brightness = 1
		wait(0.1)
	end
	light.Brightness = 0.5
end

while true do
	for _, light in pairs(script.Parent:GetDescendants()) do
		if light.ClassName == "PointLight" or light.ClassName == "SpotLight" or light.ClassName == "SurfaceLight" then
			blink(light)
		end
	end
end

that still dont blink all lights at same time :thinking:

local function flicker(light)
	while task.wait(1) do
        light.Brightness = math.random(0, 9) / 10
    end
end

for _, light in script.Parent:GetDescendants() do
	if light.ClassName == "PointLight" or light.ClassName == "SpotLight" or light.ClassName == "SurfaceLight" then
		task.spawn(flicker, light)
	end
end

@hollaquetalBRUH the above should work just fine.

1 Like

You’d want to use a different thread for each blink so using spawn or coroutine should work, adapted code from @zahra_y735

local lights = {}

for _,v in pairs(script.Parent:GetDescendants()) do
  if v.ClassName == "PointLight" or v.ClassName == "SpotLight" or v.ClassName == "SurfaceLight" then
    table.insert(lights, v)
  end
end

while true do
  coroutine.wrap(function()
for i = 1, #lights do
for c = 1, 0.1, -0.1 do
    lights[i].Brightness =c

end
task.wait(0.1)
for c = 0.1, 1, 0.1 do
    lights[i].Brightness =c

end
  end
end)

end

or

Very useful for running multiple lines of code in one script without interference

1 Like

You can try coroutines:

while true do
for _,v in pairs(script.Parent:GetDescendants()) do
        if v.ClassName == "PointLight" or v.ClassName == "SpotLight" or v.ClassName == "SurfaceLight" then
            coroutine.wrap(function()
            print("a")
            wait(0.1)
            v.Brightness = 0.8

            wait(0.1)
            v.Brightness = 0.5

            wait(0.1)
            v.Brightness = 0.8

            wait(0.1)
            v.Brightness = 0.5

            wait(0.1)
            v.Brightness = 0.8

            wait(0.1)
            v.Brightness = 1

            wait(0.1)
            v.Brightness = 0.8

            wait(0.1)
            v.Brightness = 0.5

            wait(0.1)
            v.Brightness = 1

            wait(0.1)
            v.Brightness = 0.8

            wait(0.1)
            v.Brightness = 0.5

            wait(0.1)
            v.Brightness = 0.8

            wait(0.1)
            v.Brightness = 0.1
            end)()
        end
    end
end

There are plenty of Variants you can do, 2 of the best ones are shown from @StraightScared and @Mystxry12

You Should generally use task.spawn() or task.defer()

task.spawn() makes it so code runs immediately, while task.defer() will take time until the next resumption cycle (To put it simply: Doesn’t run immediately)

While coroutines yes they are an option, they are generally used for other purposes as task.spawn() and task.defer() are used to fire code, and prevent yielding, coroutines give you more for thread to use and Manipulate the thread but its unlikely you’ll need all this functionality so its best to stick which task.spawn() for this purpose

If you ever want to keep track of all the Light Holding Instances (or the Lights themselves), you should use CollectionService instead of looping through the Descendants of something, it would put less on the Server as it already has a specified list of objects with CollectionService:GetTagged(), while yes, :GetDescendants() works, its slower as it has to loop through children, and then their children, and then their children, which can take a while, so it would be best to have a already made list, or a list to Apply when doing such things, to Add an Instance to a Tag, you would use AddTag and to remove, its RemoveTag.

@zahra_y735
Instead of checking if all of these items are from a certain class, just use the Class: Light
Which is a shared class for all Lighting Related Instance, Lights can refer to, it could make your code look nicer and making the Server not have to look through so many classes, when it can all be specified in one,

if val:IsA("Light") then
    -- code
end

If you are looking for a specific class, then it is recommended to do this for a specific class, not all, It is recommended you use Instance:IsA() instead of Instance.ClassName ==, :IsA() already does this for you by check if the Instance Is a certain class, Instance.ClassName is just overcomplicating things.

If you are looking for an Ordered Sequence, you should have a Table of these Sequences, and loop through the index keys.

LightSeq = {1, .5, .1} -- List of Numbers for use to use
index = 0

while true do
    index = (index % #LightSeq) + 1 -- to prevent the index from being 0
    Light.Brightness = LightSeq[index]
    task.wait(.5)
end
Small Rant on Math

The math is using what is known as Modulo %, it is used to get the remainder of Division, but here, it has a very good use, when we use it for numbers, if we have a number 3, and 5, we can do 3 % 5 which returns 3, but when we do 5 % 5, it returns 0, 6 % 5 is 1 and the process repeats itself, it effectively removes the usage of an if statement to check if a number is greater than a number to reset it.

If you want a Random Sequence of said Numbers:

LightSeq = {1, .5, .1}

Light.Brightness = LightSeq[math.random(1, #LightSeq)] -- Randomized Sequence
1 Like

Use random.new() instead.

local RandomNum = Random.new():NextNumber(1, 5)

While yes, Random.new() is an option, it isn’t recommended as it returns a number with Huge Decimal
I understand that it could be rounded using math.round, math.floor, or math.ceil, but its a bit absurd to be using especially here:

print(Random.new():NextNumber(1, 5))
--> 2.5851724110475685 (from what I got)
-- Rounded:
print(math.floor(Random.new():NextNumber(1, 5))/10) -- useless, overcomplicated
print(math.random(1,5)/10) -- faster, simple

You can use NextInteger but again:

Random.new():NextInteger(1, 5) -- same thing as math.random
math.random(1, 5) -- same thing as NextInteger

Both are fast, but one is more simple than the other.

Is this what you are wanting?
LightingExample.rbxl (42.5 KB)

image

local LightData = {
	0.8,
	0.5,
	0.8,
	0.5,
	0.8,
	1,
	0.8,
	0.5,
	1,
	0.8,
	0.5,
	0.8,
	0.1
}

local index = 1

while true do
	for _,v in pairs(script.Parent:GetDescendants()) do
		if v.ClassName == "PointLight" or v.ClassName == "SpotLight" or v.ClassName == "SurfaceLight" then
			v.Brightness = LightData[index]
		end
	end
	index = index + 1
	if index > #LightData then 
		index = 1 
	end
	task.wait(0.1)
end

Just use a spawn function. For example:

 for _,v in pairs(script.Parent:GetDescendants()) do
    if v.ClassName == "PointLight" or v.ClassName == "SpotLight" or v.ClassName == "SurfaceLight" 
 then
   print("a")
   spawn(function()
while true do
   wait(0.1)
        v.Brightness = 0.8

        wait(0.1)
        v.Brightness = 0.5

        wait(0.1)
        v.Brightness = 0.8

        wait(0.1)
        v.Brightness = 0.5

        wait(0.1)
        v.Brightness = 0.8

        wait(0.1)
        v.Brightness = 1

        wait(0.1)
        v.Brightness = 0.8

        wait(0.1)
        v.Brightness = 0.5

        wait(0.1)
        v.Brightness = 1

        wait(0.1)
        v.Brightness = 0.8

        wait(0.1)
        v.Brightness = 0.5

        wait(0.1)
        v.Brightness = 0.8

        wait(0.1)
        v.Brightness = 0.1
 end
 end)       

end
end

spawn() and wait() are Deprecated. And should be replaced with task.spawn() and task.wait(), this is also still inefficient as some other examples.

Exact, it’s work.
Thanks for all replies of you guys on my topics !

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.