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

Why isn't the fog changing with this "if" statement?

Asked by 4 years ago

So I'm trying to make a system where if the game's time is before 6:00 am or after 9:00 pm, the game would have fog appear. I run a loop to check if the game's time is 6:00 am or 9:00 pm, but the fog wouldn't appear. Here's my code:

local ct = game.Lighting.ClockTime

while true do
    wait(0.05)
    if ct <= 6 or ct >= 20 then
        game.Lighting.FogEnd = 150
    else
        game.Lighting.FogEnd = 100000
    end
end

The fog never appears at all once it hits any of those times. I am also running a Day and Night cycle script, but it doesn't do anything with fog, just the time.

0
Try using "and" instead of "or". youtubemasterWOW 2741 — 4y
0
it still doesnt work KyofuOmo 25 — 4y
0
Would I use GetPropertyChanged("ClockTime") instead of a loop? KyofuOmo 25 — 4y

2 answers

Log in to vote
1
Answered by
Sulfone 141
4 years ago
Edited 4 years ago

ct is the game.Lighting.ClockTime at the time of the script starting. ct wouldn't change automatically - the script doesn't care that it came from game.Lighting.ClockTime after the first line.

To fix this, line 1 can be moved between the wait and if statement. That way, ct would be game.Lighting.ClockTime at that time.

while true do
    wait(0.05)
    local ct = game.Lighting.ClockTime
    -- the if statement here
end

To your second question (about GetPropertyChanged("ClockTime")): it'll work as a loop, but I'll recommend switching to that. With GetPropertyChanged it'll just run whenever ClockTime changes, reducing work to do.

Ad
Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

So "and" should be used here because you want both conditions to be true when fog appears. Your main problem is you set ct as a value before the while loop. So ct never updates and stays the same and thus its checking the same number in the if statements. Just update ct inside the while loop and you are good.

Next time put a print statement in areas to check and see what it's doing. It will save you some time.

If you did a clock time changed function then you wouldn't need to do the while loop and it would automatically pass the new value. It's the same thing anyways, except it runs constantly which really doesn't need to be done.

Answer this question