Suddenly localscript day night won't work unless manually reenabled in studio play mode

Hey. Randomly this day night script quit working unless I manually disable and reenable it in Studio play mode.

-- Scripted by Kept (@xNashCookies) on 2024/05/27
repeat 
	task.wait()
until
game.Players.LocalPlayer.Character ~= nil

--// SETTINGS //--

local timeTakesSettingNight = 75 -- How much time it takes for the night to end
local timeTakesSettingDay = 300 -- How much time it takes for the day to end

--// TIME SETTINGS (Explorer Tab -> Lighting -> ClockTime) To change settings //--

local AfternoonNight = 17.85 -- When it starts getting dark (17.85 is 17:51 PM)
local morningNight = 6.25 -- From midnight to the sunrise (6.25 is 6:15 AM)

--// NOTE: If you are not familiar with coding, do not change the values below.

local tweenService = game:GetService("TweenService")
local Lighting = game:GetService("Lighting")

local currentTime = Lighting.ClockTime

local timeTakes = timeTakesSettingDay

local TimeRemove = (timeTakes * currentTime) / 24
timeTakes = timeTakes - TimeRemove


local function checkTime()
	if Lighting.ClockTime >= AfternoonNight or Lighting.ClockTime <= morningNight then
		return "Night"
	elseif Lighting.ClockTime < AfternoonNight and Lighting.ClockTime > morningNight then
		return "Day"
	end
end



while script.Parent.CanAdvanceClock.Value == true do
	print("is this even working")
	if checkTime() == "Day" then
		local cycleTween = tweenService:Create(Lighting, TweenInfo.new(timeTakes, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {ClockTime = AfternoonNight})
		cycleTween:Play()
		if script.Parent.CanAdvanceClock.Value == true then
			cycleTween.Completed:Wait()
		end
		Lighting.ClockTime += .01
		timeTakes = timeTakesSettingNight
	elseif checkTime() == "Night" then
		if Lighting.ClockTime >= AfternoonNight then
			print(timeTakes)
			local cycleTween = tweenService:Create(Lighting, TweenInfo.new(timeTakes, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {ClockTime = 24})
			cycleTween:Play()
			if script.Parent.CanAdvanceClock.Value == true then
				cycleTween.Completed:Wait()
			end
			Lighting.ClockTime += .01
			local substractTime  = ((timeTakesSettingNight * (24-AfternoonNight)) / 24)
			timeTakes = timeTakesSettingNight - substractTime
		elseif Lighting.ClockTime <= morningNight then
			local cycleTween = tweenService:Create(Lighting, TweenInfo.new(timeTakes, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {ClockTime = morningNight})
			cycleTween:Play()
			if script.Parent.CanAdvanceClock.Value == true then
				cycleTween.Completed:Wait()
			end
			Lighting.ClockTime += .01
			timeTakes = timeTakesSettingDay
		end
	end
end
1 Like

Is it possible that the value is initially false?
You can test this easily by putting a print statement at the end of the script, and seeing if it gets outputted (suggesting that the loop ended / never started)

I’ve tried removing it and haven’t had success.
Basically the script worked fine until recently.
The game enables the clock script which seems to happen, but it doesn’t actually run the loop.
I have to manually disable then reenable the script in the studio explorer window or else the loop doesn’t run. Using other scripts to enable / disable the clock script gives zero effect, it runs and prints but the loop itself doesn’t start up.
Loop only starts if the script is enabled from the explorer window in studio.

Are there any errors?
Does a print statement added right at the start of the code print?