Flickering light script

local light = script.Parent

while true do 
light.Brightness = .1
light.Brightness = .6
light.Brightness = .4


So basically I made this flickering light code for a game I’m working on, and I want to know if it is reliable for long servers (if i ever get my game popular)


Try and avoid while true do loops.

You would be better off using heartbeat for this and if you are wanting to have multiple lights, it is better to centralise the code into one script instead of having one script per object.

For this I would recommend you use collection service, allowing you to easily loop through all of the lights.

local CollectionService = game.GetService("CollectionService")
local Lights = CollectionService:GetTagged("Lights")

local AllLights = workspace.AllLights:GetChildren()

for index = 1, #AllLights do
	CollectionService:AddTag(AllLights[index], "Lights") -- AddTag(Instance, TagName)

Lights = CollectionService:GetTagged("Lights")

Then to avoid the while true do infinite loop I would use Heartbeat from RunService

local RunService = game:GetService("RunService")
local NextStep = tick() + math.random(5,60)/10

	if tick() >= NextStep then -- Only run after a certain amount of time, may want to add a random delay between lights flickering so it is even more random
		NextStep = tick() + math.random(5,60)/10
		for index = 1, #Lights do-- Loop through all of the lights
			if Lights[index].Brightness = 0.1 then
				Lights[index].Brightness = 0.4
			elseif Lights[index].Brightness = 0.6
				Lights[index].Brightness = 0.1
				Lights[index].Brightness = 0.6
			-- OR --
			Lights[index].Brightness = math.random(1,6)/10

How would I tag all the Lights? Do I rename the PointLights to “Lights” or is there something I’m not understanding.

No, you just need to navigate to each of the lights and do for each of your lights:

local Light = -- Wherever your light is
CollectionService:AddTag(Light , "Lights")
1 Like

Alright, I got it! Thank you for the help!

1 Like

I was planning to use since it wasn’t messy but I found a problem I get ServerScriptService.FlickerLights:8: Expected ‘do’ when parsing for loop, got ‘if’ (This is the second script you sent with your solution)

My bad, I made a typo in the original script and missed the do off the end of the loop - I have amended the original code. However I would recommend you learn how to read errors and fix them yourself because you will get them over the entire of your programming career no matter which language you are using.

I ended up using your code for another thing; sorry about the bad formatting at the time. I was fairly new to Lua when I wrote that code.

I would use math.random.
This would make the flicker more random and not just loop the same.

No no, not the actual flickering aspect, I used most of it as a way to get all the lights within a game.

1 Like

It is saying ‘AllLights is not a valid member of workspace’.

I’m confused why you want to use this example code, but a breakdown would more or less be, make a model; name it AllLights, parent all of your Lights to the model, and slam the first script; in serverscriptservice. Then make a second script, put the second script in it and slam it in serverscriptservice with a wait(1) at the start to prevent it from running too early. And you’re done.

1 Like

Ok thanks I feel stupid now I was just confused as to how to use it I guess :sweat_smile:

1 Like

I’m assuming this is for a horror game? If I can reccomend anything it’s to not use many of the flickering lights in the same room, or they’ll flicker at the same time due to how this is set up. And it’ll break the effect.

1 Like

It is but the room I’m wanting it in only has one light