Night mode not working properly

Basically when you trigger a prompt it changes the time of the day from day to night, and, if you trigger it again, from night to day. The problem is that the transition from night to day does not seem to work and I cant figure out the reason.
here is the script. Its parent is the proximity prompt

local lighting = game:GetService("Lighting")
local prompt = script.Parent.ProximityPrompt

prompt.Triggered:Connect(function(player)

	if prompt.ActionText == "Night" then
		
		
		-- Getting the lighting object
		local lighting = game:GetService("Lighting")

		-- Defining the time of day you want to transition to
		local timeOfDay = 24

		-- Defining the duration of the transition in seconds
		local duration = 3

		-- Getting the starting time of day
		local start = lighting.ClockTime

		-- Calculating the end time of day
		local target = (timeOfDay - math.floor(timeOfDay)) * 24

		-- Defining the rate of change
		local rate = (target - start) / duration

		-- Defining the end time
		local endTime = tick() + duration

		-- Loop until the end time is reached
		while tick() < endTime do
			-- Updating the clock time
			lighting.ClockTime = lighting.ClockTime + rate * wait()
		end

		-- Setting the final time of day
		lighting.ClockTime = target
		
		
		prompt.ActionText = "Day"
		script.Parent.Parent.Parent.Parent.Lighting.FogColor = Color3.fromRGB(85, 0, 255)
		for _, ff in workspace.SoundRegions.LobbyArea:GetChildren() do
			ff.Enabled = true
		end
	
	elseif prompt.ActionText == "Day" then
		-- Getting the lighting object
		local lighting = game:GetService("Lighting")

		-- Defining the time of day you want to transition to
		local timeOfDay2 = 10

		-- Defining the duration of the transition in seconds
		local duration = 3

		-- Getting the starting time of day
		local start = lighting.ClockTime

		-- Calculating the end time of day
		local target = (timeOfDay2 - math.floor(timeOfDay2)) * 24

		-- Defining the rate of change
		local rate = (target - start) / duration

		-- Defining the end time
		local endTime = tick() + duration

		-- Loop until the end time is reached
		while tick() < endTime do
			-- Updating the clock time
			lighting.ClockTime = lighting.ClockTime + rate * wait()
		end

		-- Setting the final time of day
		lighting.ClockTime = target
		
		prompt.ActionText = "Night"
		script.Parent.Parent.Parent.Parent.Lighting.FogColor = Color3.fromRGB(144, 228, 248)
		for _, ff in workspace.SoundRegions.LobbyArea:GetChildren() do
			ff.Enabled = false
		end
		
	end

end)

Thank you in advance for the help

1 Like

Can you explain in detail what happens when you trigger the prompt? Are there any errors? Could you add a print(prompt.ActionText) at the start of the function?

1 Like

i have 0
immagine_2023-03-23_184148518
errors in the output. Print function seems to work fine, showing the correct action text. The fact is that when night mode is enabled and I trigger the prompt to return to day, the lighting doesnt change, but the actiontext does. sorry for bad english

local target = (timeOfDay - math.floor(timeOfDay)) * 24
This will always evaluate to nil, and is used for both day and night sections of your script when setting lighting.ClockTime

1 Like

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