What is wrong with this script?

Hello!

I’m a fairly new scripter, and I’m trying to create a script that increases the fog at a certain time in-game.

I want it to have no fog at 6 am - 9 pm, and then I want the fog to appear at 9 pm - 6

Here’s the script I tried and it didn’t work:

if game.Lighting.ClockTime >= 21 then
game.Lighting.FogEnd = 15
end

if game.Lighting.ClockTime >= 1 then
game.Lighting.FogEnd = 15
end

if game.Lighting.ClockTime >= 6 then
game.Lighting.FogEnd = 10000
end

If there’s any way you could help, please tell me! Thank you!

You need to check with Lighting:GetPropertyChangedSignal("ClockTime"), an event is returned which then you can use :Connect() onto it. Function inside the connection should encompass the entire code that you have written.

What your code currently does, is a single-time execution that doesn’t check again as the game goes.

1 Like

Your problem is that you’re checking the clock time in 3 different if statements when you only need one and one of the if statements is unnecessary to begin with.

You can fix your if statements like this:

if Lighting.ClockTime >= 21 or Lighting.ClockTime < 6 then --Checks if time is 9PM or after or before 6AM, if it is FogEnd changes to 15.
	Lighting.FogEnd = 15
else --If it's not then FogEnd changes to 10000
	Lighting.FogEnd = 10000
end

Also declare Lighting as game.Lighting in your code as a variable so you don’t have to type game.Lighting multiple times in your code.

2 Likes

Thank you so much to both of you!

Is there a specific place I should put the script? Or does it not matter?

The most recommended and canon place to put the script in is ServerScriptService.

1 Like