Hey. So I’ve been working on script that checks certain value, and according to the value changes visual properties of all lights in certain folder.
while true do
wait(2)
local Blackout = game.Workspace.PowerInformationLights.Blackout.Value
if Blackout == false then
for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
e.Material = 'Neon'
e.Transparency = 0
end
else
for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
e.Material = 'SmoothPlastic'
e.Transparency = 0.8
end
end
end
The script itself works, but I was wondering, how can I make that when power is out / restored, all lights don’t immediately start shining, but they all start randomly flickering, and after while they turn on / off. Any idea how could I achieve that?
Also, I have separate script for actual SpotLights, I’ll just use this script there as well.
I have barely any Lua scripting knowledge, but I have experience with Python and JS so this is what I might do…
math.randomseed(tick()) -- makes it more random
function lightsOn()
local BlackoutVal = local Blackout = game.Workspace.PowerInformationLights.Blackout.Value
local repetitions = math.floor( math.random(5, 15) ) -- number of times it will flicker
local currentPlace = 0 -- place in the while loop
local currentTimer = math.random(0.1, 1.5) -- time before lights turn off/on
while currentPlace < repetitions do
wait(currentTimer) -- wait for the set random amount of time
BlackoutVal = true
currentTimer = math.random(0.1, 1.5) -- makes a new random timer
currentPlace = currentPlace + 1 -- moves up the while loop
end
BlackoutVal = true -- just in case the random flickering leaves it off
end
Just a sort of disclaimer, I’m not the best at Roblox scripting and this might not work. Tell me if there are any errors and I’ll see what’s causing it (probably a dumb syntax error)
Hope this helps!
local hasBeenShutDown = false
local repeats = 20
local time_between_flick = 0.3
while true do
wait(2)
local Blackout = game.Workspace.PowerInformationLights.Blackout.Value
if Blackout == false then
if hasBeenShutDown == true then
for i = 1,repeats,1 do
local r = math.random(1,3)
if r == 1 then
for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
e.Material = 'Neon'
e.Transparency = 0
end
else
for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
e.Material = 'SmoothPlastic'
e.Transparency = 0.8
hasBeenShutDown = true
end
end
wait(time_between_flick)
end
hasBeenShutDown = false
else
for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
e.Material = 'Neon'
e.Transparency = 0
end
end
else
for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
e.Material = 'SmoothPlastic'
e.Transparency = 0.8
hasBeenShutDown = true
end
end
end
That also doesn’t work. Output doesn’t say any errors, but when I change the value, nothing happenes. No change of material, no change of transparency.
try to add prints to the code to debug, since i can’t replicate your game like printing the random number, if its prints the random number that means that’s the part with for i,e in pairsz wich doesn’t work
else
for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
e.Material = 'SmoothPlastic'
e.Transparency = 0.8
hasBeenShutDown = true
end
end
while true do
wait(Max * 2) -- wait so the flickerings don't start to pile up.
-- again, would recommend a different type of wait.
local Blackout = game.Workspace.PowerInformationLights.Blackout.Value
if Blackout == false then
for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
coroutine.wrap(function() -- create a coroutine so as to not yield the loop
wait(math.random(Min, Max) -- wait a random amount of time
-- psa: would use a custom wait, as the wait queue can get filled up quickly.
e.Material = 'Neon'
e.Transparency = 0
wait(math.random(Min, Max)
e.Material = 'SmoothPlastic' -- return to old state so it can flash again. Remove this and the wait above if you just want them all on when false, and just have them flicker on.
e.Transparency = 0.3
end)() -- run the coroutine
end
else
for i,e in pairs(workspace.FacilityLights.Bulbs:GetChildren()) do
e.Material = 'SmoothPlastic'
e.Transparency = 0.3
end
end
end
This may not be what you want, but hopefully it gets you on the right path.
Any errors in the console? Are you actually running the function I made, because this won’t do anything if you just put it in a script. You have to run lightsOn() whenever the lights turn on for my function to be used.