Lighting script not working in Roblox player

Hello! I want to make my game more realistic by making it darker at night and lighter during the day. After figuring out how to do this, I made a script and it worked inside Roblox studio.

However, whenever I publish my game to Roblox and play it from the Roblox browser, the lighting script does not work. I feel if it works in Roblox studio it should also work in Roblox player.

I have tried modifying the script below and set the start time to 12. I’ve also tried looking around the DevForum but with no luck in finding solutions. It should also be noted I am not the best at scripting. Could this be an issue with my script or Roblox?

This is the script I’ve been using:

while true do
	if game.Lighting.ClockTime == 17.50 then
		game.Lighting.Ambient = Color3.fromRGB(90,90,90)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(90,90,90)
		game.Lighting.Brightness = 2.7
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(80,80,80)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(80,80,80)
		game.Lighting.Brightness = 2.4
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(70,70,70)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(70,70,70)
		game.Lighting.Brightness = 2.1
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(60,60,60)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(60,60,60)
		game.Lighting.Brightness = 1.8
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(50,50,50)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(50,50,50)
		game.Lighting.Brightness = 1.5
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(40,40,40)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(40,40,40)
		game.Lighting.Brightness = 1.2
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(30,30,30)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(30,30,30)
		game.Lighting.Brightness = 0.9
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(20,20,20)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(20,20,20)
		game.Lighting.Brightness = 0.6
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(10,10,10)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(10,10,10)
		game.Lighting.Brightness = 0.3
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(0,0,0)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(0,0,0)
		game.Lighting.Brightness = 0
		wait(1)
	elseif game.Lighting.ClockTime == 6 then
		game.Lighting.Ambient = Color3.fromRGB(10,10,10)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(10,10,10)
		game.Lighting.Brightness = 0.3
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(20,20,20)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(20,20,20)
		game.Lighting.Brightness = 0.6
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(30,30,30)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(30,30,30)
		game.Lighting.Brightness = 0.9
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(40,40,40)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(40,40,40)
		game.Lighting.Brightness = 1.2
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(50,50,50)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(50,50,50)
		game.Lighting.Brightness = 1.5
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(60,60,60)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(60,60,60)
		game.Lighting.Brightness = 1.8
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(70,70,70)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(70,70,70)
		game.Lighting.Brightness = 2.1
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(80,80,80)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(80,80,80)
		game.Lighting.Brightness = 2.4
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(90,90,90)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(90,90,90)
		game.Lighting.Brightness = 2.7
		wait(5)
		game.Lighting.Ambient = Color3.fromRGB(100,100,100)
		game.Lighting.OutdoorAmbient = Color3.fromRGB(100,100,100)
		game.Lighting.Brightness = 3
		wait(1)
	end
	wait(1)
end

I assume you already have something that adds to ClockTime.

What I think is happening begins with this line here:

if game.Lighting.ClockTime == 17.50 then

Now, this is valid syntax. However, there’s this common problem in computing known as ‘floating point imprecision’. In simplest terms, when a computer deals with storing and calculating decimal numbers (or real numbers) rounding errors occurs.

Long story short, you more than likely have a number like this stored in memory:

--//clock time is expected to be 17.50, but might actually be
17.5000000001 --//rough example

So comparing decimal numbers to each other doesn’t usually work for this reason.

Try a range check instead. (see if the current ClockTime is larger or equal to 17.50, but smaller than some other number.)

Revised logic:

if game.Lighting.ClockTime >= 17.50 and game.Lighting.ClockTime <= 18 then
--//execute same code
end

See if that does the trick.

I’d also like to help you rewrite a system that scales lighting based on the current clock time, but I just wanted to help you figure out this specific problem first.

Cheers.

1 Like

Along with the things @C_Sharper said, you should also utilize loops such as repeat loops so you don’t have to repeat code. You should also use functions to connect events when you can.
Here’s the fixed code:

--//Services
local Lighting = game:GetService("Lighting")

--//Functions
Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	if Lighting.ClockTime >= 17.5 then
		Lighting.Ambient = Color3.fromRGB(90, 90, 90)
		Lighting.OutdoorAmbient = Color3.fromRGB(90, 90, 90)
		Lighting.Brightness = 2.7
		
		repeat
			Lighting.Ambient = Color3.fromRGB(Lighting.Ambient.R - 10, Lighting.Ambient.B - 10, Lighting.Ambient.G - 10)
			Lighting.OutdoorAmbient = Color3.fromRGB(Lighting.Ambient.R - 10, Lighting.Ambient.B - 10, Lighting.Ambient.G - 10)
			Lighting.Brightness -= 0.3

			task.wait(5)
		until Lighting.Brightness <= 0

		task.wait(1)
		elseif Lighting.ClockTime <= 6 then
		Lighting.Ambient = Color3.fromRGB(10, 10, 10)
		Lighting.OutdoorAmbient = Color3.fromRGB(10, 10, 10)
		Lighting.Brightness = 0.3

		repeat
			Lighting.Ambient = Color3.fromRGB(Lighting.Ambient.R + 10, Lighting.Ambient.B + 10, Lighting.Ambient.G + 10)
			Lighting.OutdoorAmbient = Color3.fromRGB(Lighting.Ambient.R + 10, Lighting.Ambient.B + 10, Lighting.Ambient.G + 10)
			Lighting.Brightness += 0.3

			task.wait(5)
		until Lighting.Brightness >= 3
		
		task.wait(1)
	end
end)

You should also use task.wait(number) instead of wait(number) as task.wait(number) is more efficient and accurate: Task.wait(n) vs wait(n)

1 Like

Thanks for the tip! I’ll keep that in mind.

Thank you! I created a function similar to yours and the script works now.