Day/Night Script Help

Hello!

Basically, I want to make a script that will make a part’s color dark gray during the day, and white at night. (It’s meant for a window)

How can I accomplish this?

You can accomplish this by doing something like:

local LIGHTING = game:GetService("Lighting")

local part = script.Parent

local ColorDuringDay = Color3.fromRGB(255, 255, 255)
local ColorDuringNight = Color3.fromRGB(1, 1, 1)

while (task.wait()) do
	if (LIGHTING.ClockTime < 6 or LIGHTING.ClockTime >= 18) then
		part.Color = ColorDuringNight
	else
		part.Color = ColorDuringDay
	end
end

Might be smarter though to have it change only when ClockTime updates though if you’re doing this for multiple parts so you can reserve resources.

1 Like
local part = script.Parent
local lighting = game:GetService('Lighting')

local controlGroup = 14 --"day" time

local ColorDuringDay = Color3.fromRGB(255, 255, 255)
local ColorDuringNight = Color3.fromRGB(1, 1, 1)

lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
local ct = lighting.ClockTime
local dist = math.abs(ct-controlGroup)
part.Color = ColorDuringNight:Lerp(ColorDuringDay, dist/12)
end)

If I’m not mistaken, adding on to @Nickaladormz’s script this should create a smooth interpolation if that’s what you want.

You can also do it by inserting this script into ServerScriptService:

local Lighting = game:GetService("Lighting")

coroutine.resume(coroutine.create(function()
	while true do
		Lighting.ClockTime += 0.1
		task.wait(1)
	end
end))

This worked for me, so I hope it works for you!

Why is this a coroutine? Is there something I’m missing?

It’s a better way to schedule the day/night cycle transition. Try it and see. It actually works!