Lighting Detector Doesn't Go Off When Conditions Met

Hello!

After trial and error, I keep on winding up on the same spot: working code but not working ifs/thens. This code is supposed to change the color of the terrain when the TimeOfDay reaches a certain point. The ifs/thens don’t seem to be picking them up, though. The code goes as follows:

local terrain = game.Workspace.Terrain

local Time = game.Lighting.TimeOfDay

local daynight = script.Parent.DayNight.Value

local TimeOfDay = game.Lighting:GetMinutesAfterMidnight()

print(“gotpastcode3”)

local colorValue = Instance.new(“Color3Value”)

colorValue.Value = terrain:GetMaterialColor(“Grass”)

colorValue.Parent = script.Parent

print(“gotpastcode2”)

colorValue:GetPropertyChangedSignal(“Value”):connect(function()

terrain:SetMaterialColor(“Grass”, colorValue.Value)

end)

print(“gotpastcode1”)

local tweenInfo = TweenInfo.new(60, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)

local Night = game:GetService(“TweenService”):Create(colorValue, tweenInfo, {Value = Color3.fromRGB(91, 122, 380 )})

local Day = game:GetService(“TweenService”):Create(colorValue, tweenInfo, {Value = Color3.fromRGB(110, 148, 70)})

print(“got past code”)

while true do

print(“Looped”)

if TimeOfDay >= 21 and daynight == 0 then

Night:Play()

print(“Tweened”)

daynight = 1

end

if TimeOfDay >= 6 and daynight == 1 and TimeOfDay <= 7 then

Day:Play()

print(“Went back”)

daynight = 0

wait(0.1)

end

wait(0.1)

end

Any help will be appreciated
Thanks,

R3M1X

There are more efficient ways of doing what you are trying to do, however this will work.

When you define the variable if sets the value to the current state of time.
This value will never change until you re-define it again.

local TimeOfDay = game.Lighting:GetMinutesAfterMidnight()
print(TimeOfDay) -- returns 840
wait(5)
print(TimeOfDay) -- returns 840

Here when I print the variable at the beginning it gives me the same value as when I print the variable 5 seconds later. Because nothing has changed the value again.

local TimeOfDay = game.Lighting:GetMinutesAfterMidnight()
print(TimeOfDay) -- returns 840
wait(5)
TimeOfDay = game.Lighting:GetMinutesAfterMidnight()
print(TimeOfDay) -- returns 900

Although here I change the value right before I need to use the information and the printed output is different.

All you need to do is to update the variable every time you run through the loop.

while true do 

    TimeOfDay = game.Lighting:GetMinutesAfterMidnight() --This line

    print("Looped")
    if TimeOfDay >= 21 and daynight == 0 then
        Night:Play()
        print("Tweened")
        daynight = 1
    end
-- more stuff
end

Thanks for the help and well thought out comment. Thanks!

R3M1X

1 Like