Daylight cycle flashing

i tried making my own linear daylight cycle system, and it works fine until the split between daytime and nighttime, because it starts flashing and flickering really fast between daytime and nighttime, i dont know why this happens?? heres the code sample:

--THIS IS A LOCAL SCRIPT
local length = {
	day = workspace.Map:GetAttribute("DayLength"),
	night = workspace.Map:GetAttribute("NightLength")
}

game:GetService("RunService").Heartbeat:Connect(function()
	local daytime = game.Lighting.ClockTime >= 6 and game.Lighting.ClockTime < 18
	game.Lighting.ClockTime = (7 + workspace.DistributedGameTime / length[if daytime then "day" else "night"] * 12) % 24
end)

to be clear, both the attributes are set as how long would this half of the day take in seconds, so i assume it has something to do with the weird “formula” i came up with on the second last line, the thing is i cant figure out what makes that flashing happen

thanks

I spoke with a certain clanker. Perhaps this could help:

local Lighting = game:GetService("Lighting")
local RunService = game:GetService("RunService")

local dayLength = workspace.Map:GetAttribute("DayLength")
local nightLength = workspace.Map:GetAttribute("NightLength")

local time = 7 -- starting time

RunService.Heartbeat:Connect(function(dt)
	local isDay = time >= 6 and time < 18
	local length = isDay and dayLength or nightLength
	
	time += (12 / length) * dt
	
	if time >= 24 then
		time -= 24
	end
	
	Lighting.ClockTime = time
end)
1 Like

or just

local day = workspace.Map:GetAttribute("DayLength")
local night = workspace.Map:GetAttribute("NightLength")

local t = 7 -- time is a predefined function so cant use it

game:GetService("RunService").Heartbeat:Connect(function(dt)
	t += 12 / (if t >= 6 and t < 18 then day else night) * dt
	t %= 24
	game.Lighting.ClockTime = t
end)

a lot of people dont like being given an ai generated solution, but thanks though

It kind of…Makes things easier, though. And faster.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.