I have a day/night cycle script inside of my game, which means the TimeOfDay property is constantly changing. My goal is to, when TimeOfDay goes over the 18.3 time - fires the event which in return, enables the night-time lighting in my game. Same goes for the day-time mode, just vice versa.
This is a server script in Workspace. I cannot find any errors, and I am quite lost on what to do.
local lightA = game.Workspace.Lighting.EntranceLighting.A.LightPart.SurfaceLight
local lightB = game.Workspace.Lighting.EntranceLighting.B.LightPart.SurfaceLight
local lightC = game.Workspace.Lighting.EntranceLighting.C.LightPart.SurfaceLight
local lightD = game.Workspace.Lighting.EntranceLighting.D.LightPart.SurfaceLight
local lightE = game.Workspace.Lighting.EntranceLighting.E.LightPart.SurfaceLight
local lightF = game.Workspace.Lighting.EntranceLighting.F.LightPart.SurfaceLight
local lightG = game.Workspace.Lighting.EntranceLighting.G.LightPart.SurfaceLight
local lightH = game.Workspace.Lighting.EntranceLighting.H.LightPart.SurfaceLight
local lightI = game.Workspace.Lighting.EntranceLighting.I.LightPart.SurfaceLight
local lightJ = game.Workspace.Lighting.EntranceLighting.J.LightPart.SurfaceLight
local lightK = game.Workspace.Lighting.EntranceLighting.K.LightPart.SurfaceLight
local lightL = game.Workspace.Lighting.EntranceLighting.L.LightPart.SurfaceLight
print("Starting Lighting Cycle...")
local timeofday = game.Lighting.TimeOfDay
game.Lighting:GetPropertyChangedSignal("TimeOfDay"):Connect(function()
if timeofday == "18:30:00" then
print("Night-Mode Enabled!")
lighting.Brightness = 0
lighting.OutdoorAmbient = Color3.fromRGB(116, 116, 116)
lighting.Ambient = Color3.fromRGB(156, 156, 156)
lightA.Enabled = true
lightB.Enabled = true
lightC.Enabled = true
lightD.Enabled = true
lightE.Enabled = true
lightF.Enabled = true
lightG.Enabled = true
lightH.Enabled = true
lightI.Enabled = true
lightJ.Enabled = true
lightK.Enabled = true
lightL.Enabled = true
end
end)
local timeofday = game.Lighting.TimeOfDay
game.Lighting:GetPropertyChangedSignal("TimeOfDay"):Connect(function()
if timeofday == "06:24:00" then
print("Day-Mode Enabled!")
lighting.Brightness = 2
lighting.OutdoorAmbient = Color3.fromRGB(207, 207, 207)
lighting.Ambient = Color3.fromRGB(163, 163, 163)
end
end)```
The first thing I see is the over use of variables. Try putting all of them in a folder and use GetChildren() .
Pretty painful to see all that. Use a for loop for it.
so:
for i, v in pairs(game.Workspace.LightFolder:GetChildren()) do
v.Enabled = true
end
v is the object, the i is the placement basically but I wont go into full detail. Watch videos or articles for more info. (dont copy the script btw wont work because of the device im using)
Iâll give this a shot. I was actually wondering how to make more efficient code by not using as many variables. And as for the TimeOfDay problem, do you happen to know the problem?
Hello, if youâre using GetChildren() then consider inserting them all into a table for easy access, incase you were planning on using them in the future.
The code is working, and it is printing the message you imported - but the lighting is still not changing, and the lights are still not activating. There are still no errors. I am completely lost here, honestly.
A bit new to coding here - what kind of variable would I use to prevent it from overloading the server? It is currently doing exactly that and just repeating the code endlessly.
You would use an array and it would looks something like this
-- put them in under the same parent
-- you could also use collection service but that's a bit too long to explain
lights = lightsFolder:GetChildren()
-- when you want to change the lights
for i, v in ipairs(lights) do
v.Enabled = true
end
This code works; but is still giving the same outcome with no errors. The lighting isnât changing, and it is overloading the server by continuously printing the messages. It looks almost perfect, but still doesnât work.