while true do
wait(.1)
light.Brightness = .1
wait(2)
light.Brightness = .6
wait(0)
light.Brightness = .4
end
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)
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)
end
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
RunService.Heartbeat:Connect(function()
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
else
Lights[index].Brightness = 0.6
end
-- OR --
Lights[index].Brightness = math.random(1,6)/10
end
end
end)
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’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.
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.