TimeOfDay changing, but Event is not firing..help?

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)```

Hmmmm, try changing the

if timeofday == "18:30:00" then

to:

if timeofday == 18.3 then

If the above code doesn’t work try this:

if timeofday == 18:30:00 then

I think it is because you have it writen as a string value while the time of day is a float value.

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)
2 Likes

When attempting to change these values, it gives me a syntax error ‘’ expected identified’’ - quite confusing.

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?

Oh, I think I know a way to fix this. Firstly, can you add this one line of code, right at the start of your script:

print(typeof(game.Lighting.TimeOfDay))

and let me know what prints out.

20:47:39.601 string - Server - Script:16 - that is what was printed.

I believe timeofday requires a bit more conversion to use since you’re attempting to also string format it in the process as well

Try creating a variable using GetMinutesAfterMidnight

local lighting = game:GetService("Lighting")
print("Starting Lighting Cycle...")

game.Lighting:GetPropertyChangedSignal("TimeOfDay"):Connect(function()
    print("Check to make sure the event is firing every time the time of day gets changed")
    local TimeConversion = game.Lighting:GetMinutesAfterMidnight()
    print(TimeConversion)
    if TimeConversion == "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

    elseif TimeConversion == "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)

Also do consider figuring out how to use the GetChildren function, it’d help you organize your code a whole lot smoother :wink:

2 Likes

Awesome. I’ll give this a shot, and research the GetChildren function while I am at it. I appreciate you!

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.

1 Like

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.

You might want to try having a time frame window rather than just a single point, as if the server lags it may skip over. Example here:

local lighting = game:GetService("Lighting")
print("Starting Lighting Cycle...")

game.Lighting:GetPropertyChangedSignal("TimeOfDay"):Connect(function()
    print("Check to make sure the event is firing every time the time of day gets changed")
    local TimeConversion = game.Lighting:GetMinutesAfterMidnight()
    print(TimeConversion)
    if TimeConversion <= "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

    elseif TimeConversion >= "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)

You can also use a variable to make sure it isnt going off every tick to not overload the server.

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.

Also, there is an error via this line - Workspace.Script:22: attempt to compare number and string

You could use something like,

local AlreadyLoaded = false

and to implement it, it would look like this.

local NightLoaded = false
local DayLoaded = false

local lighting = game:GetService("Lighting")
print("Starting Lighting Cycle...")

game.Lighting:GetPropertyChangedSignal("TimeOfDay"):Connect(function()
    print("Check to make sure the event is firing every time the time of day gets changed")
    local TimeConversion = tonumber(game.Lighting:GetMinutesAfterMidnight())
    print(TimeConversion)
    if TimeConversion <= "18:30:00" then
       if not NightLoaded then
DayLoaded = false
NightLoaded = true
        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
    elseif TimeConversion >= "06:24:00" then
    if not DayLoaded then
NightLoaded = false
DayLoaded = true
        print("Day-Mode Enabled!")
        lighting.Brightness = 2
        lighting.OutdoorAmbient = Color3.fromRGB(207, 207, 207)
        lighting.Ambient = Color3.fromRGB(163, 163, 163)
end
end
end)

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

My mistake, try using tonumber(TimeConversion)

That won’t work, you’d still be comparing a number to a string (TimeConversion = Number, “18:30:00” = String)

I just realized that GetMinutesAfterMidnight() returns a numeric value, but not a string

I think we could still work around this though, try this?

local lighting = game:GetService("Lighting")
print("Starting Lighting Cycle...")

game.Lighting:GetPropertyChangedSignal("TimeOfDay"):Connect(function()
    print("Check to make sure the event is firing every time the time of day gets changed")
    local TimeConversion = game.Lighting:GetMinutesAfterMidnight()
    print(TimeConversion)
    if TimeConversion == 1080 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

    elseif TimeConversion == 384 then
        print("Day-Mode Enabled!")
        lighting.Brightness = 2
        lighting.OutdoorAmbient = Color3.fromRGB(207, 207, 207)
        lighting.Ambient = Color3.fromRGB(163, 163, 163)
    end
end)

I know this isn’t the exact approach but it’s a temporary solution for now

1 Like

I see, apologies for not testing it as I assumed GetMinutedAfterMidnight() would produce a string of numbers.

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.