Day/night cycle script does not work

my title says it. I really don’t know why it doesn’t work.
also is there a better way to make this script besides checking every 5 secs if it’s night?

local CollectionService = game:GetService("CollectionService")
local NightLights = CollectionService:GetTagged("nightLight")
local Lighting = game:GetService("Lighting")


local debounce = ""
while true do
	task.wait(5)
	if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then -- day
		if debounce ~= "day" then
			for _, NightLight in NightLights do
				task.spawn(function()
					NightLight.Brightness = 0
					NightLight.Parent.Color = Color3.new(0.266667, 0.180392, 0)
					debounce = "day"
				end)
			end
		end
	elseif game.Lighting:GetMinutesAfterMidnight() > 18 * 60 then -- night
		if debounce ~= "night" then
			for _, NightLight in NightLights do
				task.spawn(function()
					NightLight.Brightness = 1
					NightLight.Parent.Color = Color3.new(0.745098, 0.509804, 0)
					debounce = "night"
				end)
			end
		end
	end
end


This is very inefficient. You should only compute it in one function, and if it’s night, just change all the lights. Instead of computing it on every light, constantly.

1 Like

you can do something like:

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

local function changeLightToNight()
 --do your code
end

local function changeLightToDay()
 --do your code
end

local function advanceTime(timeAmount)
   --add time
   --check if new time threshold and call the respective functions to handle it
end

local function start()
 RunService.Heartbeat:Connect(function(deltaTime)
  advanceTime(deltaTime)
 end)
end

Thanks for the suggestion, fixed my code.

That is not what I asked for, I do not want to change my day/night system. I want to know what is wrong with my script.
Also why is there RunService in your script?

Exactly what is not working?

Edit : also I am confused I think no matter the case your code will only run to change to day and turn off lights. Don’t think it’ll turn off the lights try changing the order 18* 60 and 6* 60 and then also the code inside

the script does not turn on the lights

what does this mean

sorry, That’s what i meant. They won’t turn on the lights and will only turn off the lights. and to witch your thingy like the one below since if the minutes are bigger than 18*60 that will mean it’s also bigge than 6*60 so it wll always check 6*60 since you put that first.

Also I’d recommend you making it a localscript to not cause unwanted lag.


Corrected Version :

if game.Lighting:GetMinutesAfterMidnight() > 18 * 60 then -- night
		
elseif game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then -- day
		
end

Improved Version [I Guess] :

I am sure there are better ways...
Oh and i went a head to change some Variables to my liking as they confused my sense of reading or something.
local collectionService : CollectionService = game:GetService("CollectionService")
local lighting : Lighting = game:GetService("Lighting")

local nightLights = collectionService:GetTagged("nightLight")
local currentTime : string = ""

while task.wait(5) do
	local oldTime = currentTime
	currentTime = (lighting:GetMinutesAfterMidnight() > 18 * 60 or lighting:GetMinutesAfterMidnight() < 6*60) and 
		"Night" or "Day" -- less than 6*60 or bigger than 18*60 and considering it Night else Day
		
	if currentTime ~= oldTime then
		for _, NL : NightLight in nightLights do
			if not NL:IsA("Light") then continue end
			NL.Brightness = (currentTime == "Night") and 1 or 0 -- Set Color for light

			local parentPart = NL.Parent
			if parentPart and typeof(parentPart) == "Instance" and parentPart:IsA("BasePart") then -- Seting color if parent exists
				NL.Parent.Color = (currentTime == "Night") and Color3.fromRGB(190, 130, 0) or Color3.fromRGB(62, 42, 0)
			end
		end
	end

	-- You can add a code to wait for a certain time if the time till the change of day/night is too long
end

I forgot to write down that part and my comment ended not being clear enough.
I just edited it, so you can now see why.

I dont think that use while in this case is the best thing to do. What if you want do stop the time counting for a while for some reason in the future? So i did it using the logic above.

If you have any other questions, write it down and i will try to help you.

1 Like