Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

3 comments

0

Try using "and" instead of "or".

0

it still doesnt work

0

Would I use GetPropertyChanged("ClockTime") instead of a loop?

2 answers

✓ Accepted answer

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.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.