For my Backrooms game, I am developing a script that will randomly cause lights to flicker, as of right now it only has functions to toggle the lights and do RNG for causing the lights to flicker which currently just prints the random value but the light off function doesn’t work properly and the RNG function doesn’t seem to work at all. Here’s the script:
light = script.Parent
lightSource = light.SurfaceLight
function lighton()
lightSource.Enabled = true
light.Color = {211, 190, 150}
light.Material = "Neon"
end
function lightoff()
lightSource.Enabled = false
light.Color = {132, 119, 94}
light.Material = Enum("Smooth Plastic")
end
lightoff()
while true do
val = math.random(0, 100)
print(val)
task.wait(3)
end
Here’s how the light looks when it turns off (The function should also change the colour and material but only turns the light off):
And here’s the output (There should be a bunch of random numbers here from the RNG loop):
If you’re changing the Material of the part using enumerate then you must choose a type of an enumeration, so, for your solution it would be: Enum.Material
Enum is simply a group of values, which you can use for your purposes.
Here’s fixed variant:
light = script.Parent
lightSource = light.SurfaceLight
function lighton()
lightSource.Enabled = true
light.Color = {211, 190, 150}
light.Material = Enum.Material.Neon
end
function lightoff()
lightSource.Enabled = false
light.Color = {132, 119, 94}
light.Material = Enum.Material.SmoothPlastic
end
lightoff()
while true do
val = math.random(0, 100)
print(val)
task.wait(3)
end
The “Color” and “Material” format is wrong. For color, you should use Color3 and for material, you should use Enum.Material. You’re also triggering the light only once so you should add a rng loop. This should work:
local light = script.Parent
local lightSource = light.SurfaceLight
local lightOn = false
function lighton()
lightSource.Enabled = true
light.Color = Color3.fromRGB(211, 190, 150)
light.Material = Enum.Material.Neon
lightOn = true
end
function lightoff()
lightSource.Enabled = false
light.Color = Color3.fromRGB(132, 119, 94)
light.Material = Enum.Material.SmoothPlastic
lightOn = false
end
while true do
if lightOn == true then
lightoff()
else
lighton()
end
task.wait(math.random(1,3))
end
The following code should randomly turn the lights on/off every 1-3 seconds.
I’d recommend you to not use dozens of script for each part because of bad sorting and bad optimizations if you do so.
You can create a folder with all parts (light) that are suppose to be affected by the RNG system, use table so you can simply insert them all into and you can pick light with an limited amount from a table using index. Here’s e.g:
-- Defining our folder as "Folder" and getting all childrens from there, so we can iterate objects from it easily.
local Folder = script.Parent:GetChildren()
-- Creating first table which will be specified only for parts, no other objects.
local LightParts = {}
-- Now, we need to iterate through whole folder to find all instances which is classified as "Part".
for _, obj in pairs(Folder) do
if obj:IsA("Part") then
table.insert(LightParts, obj)
end
end
-- All your functions from your original code.
function lighton(light:Part, lightSource:Light)
lightSource.Enabled = true
light.Color = Color3.fromRGB(211, 190, 150)
light.Material = Enum.Material.Neon
end
function lightoff(light:Part, lightSource:Light)
lightSource.Enabled = false
light.Color = Color3.fromRGB(132, 119, 94)
light.Material = Enum.Material.SmoothPlastic
end
-- Setup your configuration right here:
local Configuration = {
-- PartLimit: Limit count for a part to be picked in RNG. (This is used to prevent too much flickering)
-- MaximumCountOfParts: Maximum count of parts which will be flickering at the same time.
PartLimit = 5;
MaximumCountOfParts = #LightParts;
-- Duration: Time for light to flick in seconds
-- TimeForFlickerEvent: Time which will delay the "while true do" in seconds
Duration = 0.25;
TimeForFlickerEvent = 60;
}
-- Don't touch it, we will need it for later.
local FlickeringParts = {}
local CurrentLimit = 0
local RNG = 0
-- Now for the main program.
while true do
-- Getting parts from LightParts to sort them into FlickeringParts table.
for _ = 1, Configuration.PartLimit do
local remainingParts = {}
for _, part in pairs(LightParts) do
if not FlickeringParts[part] then
table.insert(remainingParts, part)
end
end
if #remainingParts > 0 then
local RandomIndex = math.random(1, #remainingParts)
local selectedPart = remainingParts[RandomIndex]
table.insert(FlickeringParts, selectedPart)
CurrentLimit = CurrentLimit + 1
if CurrentLimit >= Configuration.PartLimit then
break -- Exiting loop if CurrentLimit reached to Configurated limit.
end
else
break -- Exiting 1st loop if there is no remaining parts.
end
end
-- Using parts from FlickeringParts table, not from LightsPart.
for _, part in pairs(FlickeringParts) do
local Light = part:FindFirstChildOfClass("PointLight")
if Light then
local RNG = math.random(0, 1)
if RNG == 1 then
lightoff(part, Light)
wait(Configuration.Duration)
lighton(part, Light)
end
end
end
-- Clearing all variables.
table.clear(FlickeringParts)
CurrentLimit = 0
wait(Configuration.TimeForFlickerEvent)
end