Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to check the Lightings time?

Asked by 10 years ago

So I have a street light that I want to turn on and off at certain times but it's not really working. I have made a simple day and night script that increases the Lightings TimeOfDay by 1. Anyways, this Street Light never goes on. By default it is turned off. Here is the code I got so far. The output has shown nothing wrong with it.

local Lightime = script.Parent.PointLight.Enabled

function Daynight(hit)
    if game.Lighting.TimeOfDay >= 18
         then
        if Lightime == false then
        Lightime = true
    elseif
        game.Lighting.TimeOfDay >= 6 then
        if Lightime == true then
        Lightime = false
            end
        end
    end
end

Should I just add a loop so it is constantly checking or what?

0
Woops, ignore the (hit) in the function. Not sure why I put that there but it has been removed. CrispyBrix 113 — 10y
0
Woops I also just realized I never called the function. Anyways, I am not sure what I would use to call it anyways. CrispyBrix 113 — 10y

1 answer

Log in to vote
0
Answered by 10 years ago
local Lightime = script.Parent.PointLight.Enabled

function Daynight()
    if game.Lighting.TimeOfDay >= '18:00:00'  then
        if Lightime == false then
            Lightime = true
        end
    elseif game.Lighting.TimeOfDay >= '06:00:00' then
        if Lightime == true then
            Lightime = false
        end
    end
end

game.Lighting.Changed:connect(Daynight)

There's two ways to notice whether a property in the Lighting has changed. You can use the event ".Changed" or ".LightingChanged". With the difference being, if you set the property TimeOfDay to and integer .LightingChanged will fire twice because you change it to an integer and then roblox changes it to the correct stringformat: 'HH:MM:SS'. That's why I chose .Changed here, because the less code you execute, the better.

I also cleaned up your if statements, you seemed to have a few unnecessary ends in there. A great way to prevent that from happening is to keep everything from if to then on the same line. Everything inside the if statement gets an extra indent, just like I did in my code.

Furthermore, I have made sure your if statements compare to the correct string, rather than to an integer. Roblox will automatically convert an integer like 6 to the string "6", however the string "6" does not equal to "06:00:00", which is the format in which TimeOfDay is saved.

I hope this helps, let me know if you run into any trouble!

0
Well I am not sure why it didn't work. All made sense and the Output says nothing is wrong. But the PointLight in the brick didn't go on. Should this be in a Local Script? CrispyBrix 113 — 10y
0
There's two possibilities, a) the string comparison still doesn't work. you can check for this by putting prints in the if statement. b) most likely scenario is that you shouldn't set the variable to the property but to the instance and then later alter the properties: var = game.Workspace.Point light var.Enabled = true juzzbyXfalcon 155 — 10y
Ad

Answer this question