Changing simple value just absolutely breaks my script (help needed)

Hi my script is almost complete just a very inconsistent issue i need to fix that im not too happy with
this is my day and night cycle script

local Lighting = game:GetService("Lighting")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Create RemoteEvent
local nightEvent = ReplicatedStorage:FindFirstChild("NightEvent") or Instance.new("RemoteEvent", ReplicatedStorage)
nightEvent.Name = "NightEvent"

local rainEvent = ReplicatedStorage:FindFirstChild("RainEvent") or Instance.new("RemoteEvent", ReplicatedStorage)
rainEvent.Name = "RainEvent"

local stormEvent = ReplicatedStorage:FindFirstChild("StormEvent") or Instance.new("RemoteEvent", ReplicatedStorage)
stormEvent.Name = "StormEvent"

local dayDuration = 180 -- 3 minutes
local nightDuration = 900 -- 15 minutes
local fadeDuration = 10 -- Duration for smooth transition

local playerID = 321994668

-- Add or find existing day and night sounds
local daySound = Lighting:FindFirstChild("DayAmbience") or Instance.new("Sound", Lighting)
daySound.SoundId = "rbxassetid://138089070"
daySound.Looped = true
daySound.Volume = 0
daySound.Name = "DayAmbience"
daySound:Play()

local nightSound = Lighting:FindFirstChild("NightAmbience") or Instance.new("Sound", Lighting)
nightSound.SoundId = "rbxassetid://390457804"
nightSound.Looped = true
nightSound.Volume = 0
nightSound.Name = "NightAmbience"
nightSound:Play()

local intoNightSound = Lighting:FindFirstChild("IntoNight") or Instance.new("Sound", Lighting)
intoNightSound.SoundId = "rbxassetid://13061809"
intoNightSound.Looped = false
intoNightSound.Volume = 1
intoNightSound.PlaybackSpeed = 0.2
intoNightSound.Name = "IntoNight"

local itsNightSound = Lighting:FindFirstChild("ItsNight") or Instance.new("Sound", Lighting)
itsNightSound.SoundId = "rbxassetid://9043340747"
itsNightSound.Looped = false
itsNightSound.Volume = 1
itsNightSound.Name = "ItsNight"

-- Add new sounds for daytime transition
local intoDaySound1 = Lighting:FindFirstChild("IntoDay1") or Instance.new("Sound", Lighting)
intoDaySound1.SoundId = "rbxassetid://266271872"
intoDaySound1.Looped = false
intoDaySound1.Volume = 1
intoDaySound1.PlaybackSpeed = 0.9
intoDaySound1.Name = "IntoDay1"

local intoDaySound2 = Lighting:FindFirstChild("IntoDay2") or Instance.new("Sound", Lighting)
intoDaySound2.SoundId = "rbxassetid://266271872"
intoDaySound2.Looped = false
intoDaySound2.Volume = 1
intoDaySound2.PlaybackSpeed = 0.8
intoDaySound2.Name = "IntoDay2"

-- Add storm sound
local stormSound = Lighting:FindFirstChild("StormSound") or Instance.new("Sound", Lighting)
stormSound.SoundId = "rbxassetid://9120020706"
stormSound.Looped = false  -- Ensure the sound doesn't loop
stormSound.Volume = 0
stormSound.Name = "StormSound"

print("Day sound, night sound, into night sound, its night sound, and storm sound initialized and started.")
print("IntoDay sounds initialized.")

local firstDaytime = true
local isNightTransitioning = false
local isRaining = false
local isStorming = false

-- Function to show the "SURVIVE" text
local function showSurviveText()
	nightEvent:FireAllClients()
end

-- Improved function to smoothly change the lighting and audio
local function smoothTransition(targetBrightness, targetAmbient, targetOutdoorAmbient, targetClockTime, targetDayVolume, targetNightVolume, playIntoNight, playIntoDay, duration)
	local initialBrightness = Lighting.Brightness
	local initialAmbient = Lighting.Ambient
	local initialOutdoorAmbient = Lighting.OutdoorAmbient
	local initialClockTime = Lighting.ClockTime
	local initialDayVolume = daySound.Volume
	local initialNightVolume = nightSound.Volume
	local startTime = tick()
	local transitionTime = duration

	-- Restart and delay playing into night sound by 0.5 seconds
	if playIntoNight then
		intoNightSound:Stop()
		intoNightSound.TimePosition = 0
		intoNightSound.Volume = 1
		intoNightSound.PlaybackSpeed = 0.2
		spawn(function()
			wait(0.5)
			print("Playing into night sound.")
			intoNightSound:Play()
		end)
	end

	-- Restart and delay playing into day sounds
	if playIntoDay and not firstDaytime then
		intoDaySound1:Stop()
		intoDaySound1.TimePosition = 0
		intoDaySound1.Volume = 1
		intoDaySound1.PlaybackSpeed = 0.9

		intoDaySound2:Stop()
		intoDaySound2.TimePosition = 0
		intoDaySound2.Volume = 1
		intoDaySound2.PlaybackSpeed = 0.8

		spawn(function()
			wait(0.5)
			print("Playing into day sounds.")
			intoDaySound1:Play()
			intoDaySound2:Play()
		end)
	end

	while tick() - startTime < transitionTime do
		local elapsed = tick() - startTime
		local progress = elapsed / transitionTime

		-- Update lighting properties
		Lighting.Brightness = initialBrightness + (targetBrightness - initialBrightness) * progress
		Lighting.Ambient = initialAmbient:lerp(targetAmbient, progress)
		Lighting.OutdoorAmbient = initialOutdoorAmbient:lerp(targetOutdoorAmbient, progress)
		Lighting.ClockTime = initialClockTime + (targetClockTime - initialClockTime) * progress

		-- Update audio volumes
		daySound.Volume = initialDayVolume + (targetDayVolume - initialDayVolume) * progress
		nightSound.Volume = initialNightVolume + (targetNightVolume - initialNightVolume) * progress

		-- Fade out into night sound near the end
		if playIntoNight and intoNightSound.IsPlaying and elapsed > transitionTime - 1 then
			intoNightSound.Volume = (transitionTime - elapsed) / 1
		end

		-- Wait a short time before the next update
		wait(0.03) -- Update more frequently for smoother transition
	end

	-- Ensure final values are set
	Lighting.Brightness = targetBrightness
	Lighting.Ambient = targetAmbient
	Lighting.OutdoorAmbient = targetOutdoorAmbient
	Lighting.ClockTime = targetClockTime
	daySound.Volume = targetDayVolume
	nightSound.Volume = targetNightVolume
	if playIntoNight then
		intoNightSound.Volume = 0
		print("Transition completed: ClockTime =", targetClockTime, "Day Volume =", targetDayVolume, "Night Volume =", targetNightVolume)
		print("It's night now.")
		wait(1)
		itsNightSound:Play()
		nightEvent:FireAllClients()  -- Fire the event to all clients
		showSurviveText()  -- Show the "SURVIVE" text when night starts
	end
end

-- Function to start the day-night cycle
local function startDayNightCycle()
	while true do
		-- Daytime (6 AM to 6 PM)
		print("Starting daytime transition.")
		if firstDaytime then
			smoothTransition(2, Color3.fromRGB(255, 255, 255), Color3.fromRGB(128, 128, 128), 14, 0.207, 0, false, false, fadeDuration)
			firstDaytime = false
		else
			smoothTransition(2, Color3.fromRGB(255, 255, 255), Color3.fromRGB(128, 128, 128), 14, 0.207, 0, false, true, fadeDuration)
		end
		wait(dayDuration)

		-- Transition to night (6 PM to 6 AM)
		print("Starting night transition.")
		isNightTransitioning = true
		smoothTransition(0, Color3.fromRGB(0, 0, 0), Color3.fromRGB(0, 0, 0), 0, 0, 0.207, true, false, fadeDuration)
		wait(nightDuration)
		isNightTransitioning = false
	end
end

-- Function to smoothly transition storm effects
local function smoothStormTransition(action, duration)
	local initialOutdoorAmbient = Lighting.OutdoorAmbient
	local targetOutdoorAmbient = action == "start" and Color3.fromRGB(0, 0, 0) or Lighting:GetAttribute("OriginalOutdoorAmbient") or Color3.fromRGB(128, 128, 128)
	local initialBrightness = Lighting.Brightness
	local targetBrightness = action == "start" and 0 or 2
	local startTime = tick()

	if action == "start" then
		stormSound:Stop()
		stormSound.TimePosition = 0
		stormSound.Volume = 3
		stormSound.Looped = false
		stormSound:Play()
	end

	while tick() - startTime < duration do
		local elapsed = tick() - startTime
		local progress = elapsed / duration

		Lighting.OutdoorAmbient = initialOutdoorAmbient:lerp(targetOutdoorAmbient, progress)
		Lighting.Brightness = initialBrightness + (targetBrightness - initialBrightness) * progress
		stormSound.Volume = action == "start" and (3 * progress) or (3 * (1 - progress)) -- Fade in/out the storm sound

		wait(0.03)
	end

	Lighting.OutdoorAmbient = targetOutdoorAmbient
	Lighting.Brightness = targetBrightness
	if action == "stop" then
		stormSound:Stop()
	end
end

-- Function to trigger rain
local function triggerRain(action)
	local initialClockTime = Lighting.ClockTime
	for _, player in ipairs(Players:GetPlayers()) do
		if action == "start" then
			rainEvent:FireClient(player, "EnableRain")
			isRaining = true

			-- 1/30 chance to start a storm when rain starts
			if math.random(1, 30) == 1 then
				triggerStorm("start")
			end

			if not isNightTransitioning and Lighting.ClockTime >= 6 and Lighting.ClockTime < 18 then
				-- Smoothly change the sky color and ClockTime during the day
				if not Lighting:GetAttribute("OriginalOutdoorAmbient") then
					Lighting:SetAttribute("OriginalOutdoorAmbient", Lighting.OutdoorAmbient)
				end
				local initialColor = Lighting.OutdoorAmbient
				local targetColor = Color3.fromRGB(58, 58, 59)
				local fadeTime = 5 -- 5 seconds fade time
				local initialBrightness = Lighting.Brightness
				local targetBrightness = initialBrightness * 0.8 -- Slightly lower brightness during rain

				spawn(function()
					local startTime = tick()
					while tick() - startTime < fadeTime do
						local elapsed = tick() - startTime
						local progress = elapsed / fadeTime
						Lighting.OutdoorAmbient = initialColor:lerp(targetColor, progress)
						Lighting.Brightness = initialBrightness + (targetBrightness - initialBrightness) * progress
						Lighting.ClockTime = initialClockTime + (6.254 - initialClockTime) * progress -- Smoothly change the ClockTime to 6.254 during rain
						wait(0.03)
					end
					Lighting.OutdoorAmbient = targetColor
					Lighting.Brightness = targetBrightness
					Lighting.ClockTime = 6.254
				end)
			else
				-- Set the sky color instantly if it's not day
				Lighting.OutdoorAmbient = Color3.fromRGB(58, 58, 59)
				Lighting.ClockTime = 23.0443 -- Set the ClockTime to 23.0443 during rain at night
			end
		elseif action == "stop" then
			rainEvent:FireClient(player, "DisableRain")
			isRaining = false

			if not isNightTransitioning and Lighting.ClockTime >= 6 and Lighting.ClockTime < 18 then
				-- Smoothly change the sky color and ClockTime back to original during the day
				local initialColor = Lighting.OutdoorAmbient
				local targetColor = Lighting:GetAttribute("OriginalOutdoorAmbient") or Color3.fromRGB(128, 128, 128)
				local fadeTime = 7 -- 7 seconds fade time
				local initialDayVolume = daySound.Volume
				local targetDayVolume = 0.207 -- Reset to original day volume

				spawn(function()
					local startTime = tick()
					while tick() - startTime < fadeTime do
						local elapsed = tick() - startTime
						local progress = elapsed / fadeTime
						Lighting.OutdoorAmbient = initialColor:lerp(targetColor, progress)
						daySound.Volume = initialDayVolume + (targetDayVolume - initialDayVolume) * progress
						Lighting.ClockTime = initialClockTime + (13.089 - initialClockTime) * progress -- Smoothly change the ClockTime back to 13.089 after rain stops
						wait(0.03)
					end
					Lighting.OutdoorAmbient = targetColor
					daySound.Volume = targetDayVolume
					Lighting.ClockTime = 13.089
				end)
			else
				-- Change the sky color back to original instantly if it's not day
				Lighting.OutdoorAmbient = Lighting:GetAttribute("OriginalOutdoorAmbient") or Color3.fromRGB(128, 128, 128)
				Lighting.ClockTime = 23.0443 -- Keep the ClockTime at 23.0443 if rain stops at night

				-- Reset nighttime settings when rain stops at night
				Lighting.Brightness = 0
				Lighting.Ambient = Color3.fromRGB(0, 0, 0)
				Lighting.OutdoorAmbient = Color3.fromRGB(26, 26, 26)
				Lighting.ColorShift_Bottom = Color3.fromRGB(0, 0, 0)
				Lighting.ColorShift_Top = Color3.fromRGB(0, 0, 0)
				nightSound.Volume = 0.207 -- Ensure night volume is set correctly
			end
		end
	end
end

-- Function to trigger storm
local function triggerStorm(action)
	for _, player in ipairs(Players:GetPlayers()) do
		if action == "start" then
			stormEvent:FireClient(player, "EnableStorm")
			isStorming = true
			smoothStormTransition("start", 10)  -- Smooth transition for storm
			-- Additional storm-specific logic can be added here (e.g., changing skybox, more intense effects)
		elseif action == "stop" then
			stormEvent:FireClient(player, "DisableStorm")
			isStorming = false
			smoothStormTransition("stop", 10)  -- Smooth transition to stop storm
			-- Additional storm-specific reset logic can be added here
		end
	end
end

-- Function to handle manual triggering of day and night
local function onChatMessage(player, message)
	if player.UserId == playerID then
		if message == ";trigger day" then
			print("Manual trigger: day")
			smoothTransition(2, Color3.fromRGB(255, 255, 255), Color3.fromRGB(128, 128, 128), 14, 0.207, 0, false, true, fadeDuration)
		elseif message == ";trigger night" then
			print("Manual trigger: night")
			smoothTransition(0, Color3.fromRGB(0, 0, 0), Color3.fromRGB(0, 0, 0), 0, 0, 0.207, true, false, fadeDuration)
			showSurviveText() -- Show the "SURVIVE" text when manually triggering night
		elseif message == ";trigger rain" then
			print("Manual trigger: rain")
			triggerRain("start")
		elseif message == ";trigger rainstop" then
			print("Manual trigger: rainstop")
			triggerRain("stop")
		elseif message == ";trigger storm" and isRaining then
			print("Manual trigger: storm")
			triggerStorm("start")
		elseif message == ";trigger stormstop" and isStorming then
			print("Manual trigger: stormstop")
			triggerStorm("stop")
		end
	end
end

-- Connect the chat event
Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		onChatMessage(player, message)
	end)
	-- Handle ongoing rain or storm for new players
	if isRaining then
		rainEvent:FireClient(player, "EnableRain")
	end
	if isStorming then
		stormEvent:FireClient(player, "EnableStorm")
	end
end)

-- Function to handle random rain
local function randomRain()
	while true do
		wait(60) -- Wait 1 minute
		if math.random(1, 35) == 1 then
			triggerRain("start")
			local rainDuration = math.random(900, 1500) -- Random duration between 15 and 25 minutes
			wait(rainDuration)
			triggerRain("stop")
		end
	end
end

-- Start the day-night cycle
coroutine.wrap(startDayNightCycle)()

-- Start the random rain cycle
coroutine.wrap(randomRain)()

the brightness and audio glitch when transitioning from day to night and its not good
I tried to fix it but then the transition to night completely broke
Please for the love of roblox or whatever help me.

2 Likes

do you have any errors inside of the console?

2 Likes

no, i dont get any errors it just triggers the night event but the server isnt considered night.

1 Like

maybe try making debugging prints?

2 Likes

this is code i made prior with debug prints and it only helped a little

local Lighting = game:GetService("Lighting")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Create RemoteEvent
local nightEvent = ReplicatedStorage:FindFirstChild("NightEvent") or Instance.new("RemoteEvent", ReplicatedStorage)
nightEvent.Name = "NightEvent"

local rainEvent = ReplicatedStorage:FindFirstChild("RainEvent") or Instance.new("RemoteEvent", ReplicatedStorage)
rainEvent.Name = "RainEvent"

local stormEvent = ReplicatedStorage:FindFirstChild("StormEvent") or Instance.new("RemoteEvent", ReplicatedStorage)
stormEvent.Name = "StormEvent"

local dayDuration = 180 -- 3 minutes
local nightDuration = 900 -- 15 minutes
local fadeDuration = 10 -- Duration for smooth transition

local playerID = 321994668

-- Add or find existing day and night sounds
local daySound = Lighting:FindFirstChild("DayAmbience") or Instance.new("Sound", Lighting)
daySound.SoundId = "rbxassetid://138089070"
daySound.Looped = true
daySound.Volume = 0
daySound.Name = "DayAmbience"
daySound:Play()

local nightSound = Lighting:FindFirstChild("NightAmbience") or Instance.new("Sound", Lighting)
nightSound.SoundId = "rbxassetid://390457804"
nightSound.Looped = true
nightSound.Volume = 0
nightSound.Name = "NightAmbience"
nightSound:Play()

local intoNightSound = Lighting:FindFirstChild("IntoNight") or Instance.new("Sound", Lighting)
intoNightSound.SoundId = "rbxassetid://13061809"
intoNightSound.Looped = false
intoNightSound.Volume = 1
intoNightSound.PlaybackSpeed = 0.2
intoNightSound.Name = "IntoNight"

local itsNightSound = Lighting:FindFirstChild("ItsNight") or Instance.new("Sound", Lighting)
itsNightSound.SoundId = "rbxassetid://9043340747"
itsNightSound.Looped = false
itsNightSound.Volume = 1
itsNightSound.Name = "ItsNight"

-- Add new sounds for daytime transition
local intoDaySound1 = Lighting:FindFirstChild("IntoDay1") or Instance.new("Sound", Lighting)
intoDaySound1.SoundId = "rbxassetid://266271872"
intoDaySound1.Looped = false
intoDaySound1.Volume = 1
intoDaySound1.PlaybackSpeed = 0.9
intoDaySound1.Name = "IntoDay1"

local intoDaySound2 = Lighting:FindFirstChild("IntoDay2") or Instance.new("Sound", Lighting)
intoDaySound2.SoundId = "rbxassetid://266271872"
intoDaySound2.Looped = false
intoDaySound2.Volume = 1
intoDaySound2.PlaybackSpeed = 0.8
intoDaySound2.Name = "IntoDay2"

-- Add storm sound
local stormSound = Lighting:FindFirstChild("StormSound") or Instance.new("Sound", Lighting)
stormSound.SoundId = "rbxassetid://9120020706"
stormSound.Looped = false  -- Ensure the sound doesn't loop
stormSound.Volume = 0
stormSound.Name = "StormSound"

print("Day sound, night sound, into night sound, its night sound, and storm sound initialized and started.")
print("IntoDay sounds initialized.")

local firstDaytime = true
local isNightTransitioning = false
local isRaining = false
local isStorming = false
local isTransitioning = false

-- Function to show the "SURVIVE" text
local function showSurviveText()
	nightEvent:FireAllClients()
	print("SURVIVE text event fired to all clients.")
end

-- Improved function to smoothly change the lighting and audio
local function smoothTransition(targetBrightness, targetAmbient, targetOutdoorAmbient, targetClockTime, targetDayVolume, targetNightVolume, playIntoNight, playIntoDay, duration, manualNight)
	if isTransitioning then
		print("Transition already in progress, skipping new transition.")
		return
	end
	isTransitioning = true
	
	local initialBrightness = Lighting.Brightness
	local initialAmbient = Lighting.Ambient
	local initialOutdoorAmbient = Lighting.OutdoorAmbient
	local initialClockTime = Lighting.ClockTime
	local initialDayVolume = daySound.Volume
	local initialNightVolume = nightSound.Volume
	local startTime = tick()
	local transitionTime = duration

	print("Starting smooth transition...")

	-- Restart and delay playing into night sound by 0.5 seconds
	if playIntoNight then
		intoNightSound:Stop()
		intoNightSound.TimePosition = 0
		intoNightSound.Volume = 1
		intoNightSound.PlaybackSpeed = 0.2
		spawn(function()
			wait(0.5)
			print("Playing into night sound.")
			intoNightSound:Play()
		end)
	end

	-- Restart and delay playing into day sounds
	if playIntoDay and not firstDaytime then
		intoDaySound1:Stop()
		intoDaySound1.TimePosition = 0
		intoDaySound1.Volume = 1
		intoDaySound1.PlaybackSpeed = 0.9

		intoDaySound2:Stop()
		intoDaySound2.TimePosition = 0
		intoDaySound2.Volume = 1
		intoDaySound2.PlaybackSpeed = 0.8

		spawn(function()
			wait(0.5)
			print("Playing into day sounds.")
			intoDaySound1:Play()
			intoDaySound2:Play()
		end)
	end

	while tick() - startTime < transitionTime do
		local elapsed = tick() - startTime
		local progress = elapsed / transitionTime

		-- Update lighting properties
		Lighting.Brightness = initialBrightness + (targetBrightness - initialBrightness) * progress
		Lighting.Ambient = initialAmbient:lerp(targetAmbient, progress)
		Lighting.OutdoorAmbient = initialOutdoorAmbient:lerp(targetOutdoorAmbient, progress)
		Lighting.ClockTime = initialClockTime + (targetClockTime - initialClockTime) * progress

		-- Update audio volumes
		daySound.Volume = initialDayVolume + (targetDayVolume - initialDayVolume) * progress
		nightSound.Volume = initialNightVolume + (targetNightVolume - initialNightVolume) * progress

		-- Fade out into night sound near the end
		if playIntoNight and intoNightSound.IsPlaying and elapsed > transitionTime - 1 then
			intoNightSound.Volume = (transitionTime - elapsed) / 1
		end

		-- Wait a short time before the next update
		wait(0.03) -- Update more frequently for smoother transition
	end

	-- Ensure final values are set
	Lighting.Brightness = targetBrightness
	Lighting.Ambient = targetAmbient
	Lighting.OutdoorAmbient = targetOutdoorAmbient
	Lighting.ClockTime = targetClockTime
	daySound.Volume = targetDayVolume
	nightSound.Volume = targetNightVolume
	if playIntoNight then
		intoNightSound.Volume = 0
		print("Transition completed: ClockTime =", targetClockTime, "Day Volume =", targetDayVolume, "Night Volume =", targetNightVolume)
		if not manualNight then
			print("It's night now.")
			wait(1)
			itsNightSound:Play()
			nightEvent:FireAllClients()  -- Fire the event to all clients
			print("Night event fired to all clients.")
			showSurviveText()  -- Show the "SURVIVE" text when night starts
		end
	end

	isTransitioning = false
end

-- Function to start the day-night cycle
local function startDayNightCycle()
	while true do
		-- Daytime (6 AM to 6 PM)
		print("Starting daytime transition.")
		if firstDaytime then
			smoothTransition(2, Color3.fromRGB(255, 255, 255), Color3.fromRGB(128, 128, 128), 14, 0.207, 0, false, false, fadeDuration, false)
			firstDaytime = false
		else
			smoothTransition(2, Color3.fromRGB(255, 255, 255), Color3.fromRGB(128, 128, 128), 14, 0.207, 0, false, true, fadeDuration, false)
		end
		wait(dayDuration)

		-- Transition to night (6 PM to 6 AM)
		print("Starting night transition.")
		isNightTransitioning = true
		smoothTransition(0, Color3.fromRGB(0, 0, 0), Color3.fromRGB(0, 0, 0), 0, 0, 0.207, true, false, fadeDuration, false)
		wait(nightDuration)
		isNightTransitioning = false
	end
end

-- Function to smoothly transition storm effects
local function smoothStormTransition(action, duration)
	local initialOutdoorAmbient = Lighting.OutdoorAmbient
	local targetOutdoorAmbient = action == "start" and Color3.fromRGB(0, 0, 0) or Lighting:GetAttribute("OriginalOutdoorAmbient") or Color3.fromRGB(128, 128, 128)
	local initialBrightness = Lighting.Brightness
	local targetBrightness = action == "start" and 0 or 2
	local startTime = tick()

	print("Starting smooth storm transition...")

	if action == "start" then
		stormSound:Stop()
		stormSound.TimePosition = 0
		stormSound.Volume = 3
		stormSound.Looped = false
		stormSound:Play()
	end

	while tick() - startTime < duration do
		local elapsed = tick() - startTime
		local progress = elapsed / duration

		Lighting.OutdoorAmbient = initialOutdoorAmbient:lerp(targetOutdoorAmbient, progress)
		Lighting.Brightness = initialBrightness + (targetBrightness - initialBrightness) * progress
		stormSound.Volume = action == "start" and (3 * progress) or (3 * (1 - progress)) -- Fade in/out the storm sound

		wait(0.03)
	end

	Lighting.OutdoorAmbient = targetOutdoorAmbient
	Lighting.Brightness = targetBrightness
	if action == "stop" then
		stormSound:Stop()
	end

	print("Storm transition completed.")
end

-- Function to trigger rain
local function triggerRain(action)
	local initialClockTime = Lighting.ClockTime
	for _, player in ipairs(Players:GetPlayers()) do
		if action == "start" then
			print("Triggering rain...")
			rainEvent:FireClient(player, "EnableRain")
			isRaining = true

			-- 1/30 chance to start a storm when rain starts
			if math.random(1, 30) == 1 then
				spawn(function()
					wait(20) -- Wait 20 seconds into the rain
					if isRaining then
						print("Triggering storm due to rain...")
						triggerStorm("start")
					end
				end)
			end

			if not isNightTransitioning and Lighting.ClockTime >= 6 and Lighting.ClockTime < 18 then
				-- Smoothly change the sky color and ClockTime during the day
				if not Lighting:GetAttribute("OriginalOutdoorAmbient") then
					Lighting:SetAttribute("OriginalOutdoorAmbient", Lighting.OutdoorAmbient)
				end
				local initialColor = Lighting.OutdoorAmbient
				local targetColor = Color3.fromRGB(58, 58, 59)
				local fadeTime = 5 -- 5 seconds fade time
				local initialBrightness = Lighting.Brightness
				local targetBrightness = initialBrightness * 0.8 -- Slightly lower brightness during rain

				spawn(function()
					local startTime = tick()
					while tick() - startTime < fadeTime do
						local elapsed = tick() - startTime
						local progress = elapsed / fadeTime
						Lighting.OutdoorAmbient = initialColor:lerp(targetColor, progress)
						Lighting.Brightness = initialBrightness + (targetBrightness - initialBrightness) * progress
						Lighting.ClockTime = initialClockTime + (6.254 - initialClockTime) * progress -- Smoothly change the ClockTime to 6.254 during rain
						wait(0.03)
					end
					Lighting.OutdoorAmbient = targetColor
					Lighting.Brightness = targetBrightness
					Lighting.ClockTime = 6.254
					print("Rain transition completed.")
				end)
			else
				-- Set the sky color instantly if it's not day
				Lighting.OutdoorAmbient = Color3.fromRGB(58, 58, 59)
				Lighting.ClockTime = 23.0443 -- Set the ClockTime to 23.0443 during rain at night
				print("Rain started during night.")
			end
		elseif action == "stop" then
			print("Stopping rain...")
			rainEvent:FireClient(player, "DisableRain")
			isRaining = false

			if not isNightTransitioning and Lighting.ClockTime >= 6 and Lighting.ClockTime < 18 then
				-- Smoothly change the sky color and ClockTime back to original during the day
				local initialColor = Lighting.OutdoorAmbient
				local targetColor = Lighting:GetAttribute("OriginalOutdoorAmbient") or Color3.fromRGB(128, 128, 128)
				local fadeTime = 7 -- 7 seconds fade time
				local initialDayVolume = daySound.Volume
				local targetDayVolume = 0.207 -- Reset to original day volume

				spawn(function()
					local startTime = tick()
					while tick() - startTime < fadeTime do
						local elapsed = tick() - startTime
						local progress = elapsed / fadeTime
						Lighting.OutdoorAmbient = initialColor:lerp(targetColor, progress)
						daySound.Volume = initialDayVolume + (targetDayVolume - initialDayVolume) * progress
						Lighting.ClockTime = initialClockTime + (13.089 - initialClockTime) * progress -- Smoothly change the ClockTime back to 13.089 after rain stops
						wait(0.03)
					end
					Lighting.OutdoorAmbient = targetColor
					daySound.Volume = targetDayVolume
					Lighting.ClockTime = 13.089
					print("Rain stopped, transition to day completed.")
				end)
			else
				-- Change the sky color back to original instantly if it's not day
				Lighting.OutdoorAmbient = Lighting:GetAttribute("OriginalOutdoorAmbient") or Color3.fromRGB(128, 128, 128)
				Lighting.ClockTime = 23.0443 -- Keep the ClockTime at 23.0443 if rain stops at night

				-- Reset nighttime settings when rain stops at night
				Lighting.Brightness = 0
				Lighting.Ambient = Color3.fromRGB(0, 0, 0)
				Lighting.OutdoorAmbient = Color3.fromRGB(26, 26, 26)
				Lighting.ColorShift_Bottom = Color3.fromRGB(0, 0, 0)
				Lighting.ColorShift_Top = Color3.fromRGB(0, 0, 0)
				nightSound.Volume = 0.207 -- Ensure night volume is set correctly
				print("Rain stopped during night.")
			end
		end
	end
end

-- Function to trigger storm
local function triggerStorm(action)
	for _, player in ipairs(Players:GetPlayers()) do
		if action == "start" then
			print("Triggering storm...")
			stormEvent:FireClient(player, "EnableStorm")
			isStorming = true
			smoothStormTransition("start", 10)  -- Smooth transition for storm
			-- Additional storm-specific logic can be added here (e.g., changing skybox, more intense effects)
		elseif action == "stop" then
			print("Stopping storm...")
			stormEvent:FireClient(player, "DisableStorm")
			isStorming = false
			smoothStormTransition("stop", 10)  -- Smooth transition to stop storm
			-- Additional storm-specific reset logic can be added here
		end
	end
end

-- Function to handle manual triggering of day and night
local function onChatMessage(player, message)
	if player.UserId == playerID then
		if message == ";trigger day" then
			print("Manual trigger: day")
			smoothTransition(2, Color3.fromRGB(255, 255, 255), Color3.fromRGB(128, 128, 128), 14, 0.207, 0, false, true, fadeDuration, false)
		elseif message == ";trigger night" then
			print("Manual trigger: night")
			smoothTransition(0, Color3.fromRGB(0, 0, 0), Color3.fromRGB(0, 0, 0), 0, 0, 0.207, true, false, fadeDuration, true)
			print("Manual night transition completed.")
			showSurviveText() -- Show the "SURVIVE" text when manually triggering night
		elseif message == ";trigger rain" then
			print("Manual trigger: rain")
			triggerRain("start")
		elseif message == ";trigger rainstop" then
			print("Manual trigger: rainstop")
			triggerRain("stop")
		elseif message == ";trigger storm" and isRaining then
			print("Manual trigger: storm")
			triggerStorm("start")
		elseif message == ";trigger stormstop" and isStorming then
			print("Manual trigger: stormstop")
			triggerStorm("stop")
		end
	end
end

-- Connect the chat event
Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		onChatMessage(player, message)
	end)
	-- Handle ongoing rain or storm for new players
	if isRaining then
		rainEvent:FireClient(player, "EnableRain")
	end
	if isStorming then
		stormEvent:FireClient(player, "EnableStorm")
	end
end)

-- Function to handle random rain
local function randomRain()
	while true do
		wait(60) -- Wait 1 minute
		if math.random(1, 35) == 1 then
			print("Randomly triggering rain...")
			triggerRain("start")
			local rainDuration = math.random(900, 1500) -- Random duration between 15 and 25 minutes
			wait(rainDuration)
			print("Randomly stopping rain...")
			triggerRain("stop")
		end
	end
end

-- Start the day-night cycle
coroutine.wrap(startDayNightCycle)()

-- Start the random rain cycle
coroutine.wrap(randomRain)()

print("Day-night and random rain cycles started.")

this was the output

  12:50:43.854  Horror survival auto-recovery file was created  -  Studio
  12:50:44.058  Day sound, night sound, into night sound, its night sound, and storm sound initialized and started.  -  Server - DayNightCycle:71
  12:50:44.058  IntoDay sounds initialized.  -  Server - DayNightCycle:72
  12:50:44.058  Starting daytime transition.  -  Server - DayNightCycle:188
  12:50:44.058  Starting smooth transition...  -  Server - DayNightCycle:103
  12:50:44.058  Day-night and random rain cycles started.  -  Server - DayNightCycle:423
  12:50:44.059  It's day now. Despawning AI...  -  Server - MainScript:495
  12:50:44.129  A player joined!  -  Server - PlayerJoin:6
  12:50:44.485  Removed head from Troublemaiker  -  Client - InvisHead:13
  12:50:44.485  Successfully disabled the reset button!  -  Client - DisableReset:12
  12:50:44.486  Disabled display names for Troublemaiker  -  Client - DisableDisplayName:26
  12:50:44.498   â–¶ stopRunning called (x2)  -  Client - StaminaScript:111
  12:50:51.919  Manual trigger: night  -  Server - DayNightCycle:368
  12:50:51.919  Transition already in progress, skipping new transition.  -  Server - DayNightCycle:89
  12:50:51.919  Manual night transition completed.  -  Server - DayNightCycle:370
  12:50:51.919  SURVIVE text event fired to all clients.  -  Server - DayNightCycle:83
  12:50:59.486  Manual trigger: night  -  Server - DayNightCycle:368
  12:50:59.486  Starting smooth transition...  -  Server - DayNightCycle:103
  12:51:00.019  Playing into night sound.  -  Server - DayNightCycle:113
  12:51:05.219  It's night now. Spawning AI...  -  Server - MainScript:487
  12:51:09.518  Transition completed: ClockTime = 0 Day Volume = 0 Night Volume = 0.207  -  Server - DayNightCycle:170
  12:51:09.518  Manual night transition completed.  -  Server - DayNightCycle:370
  12:51:09.519  SURVIVE text event fired to all clients.  -  Server - DayNightCycle:83
  12:51:15.452  AI humanoid spawned at: -86.82501220703125, 5.449967384338379, 102.94001007080078  -  Server - MainScript:433
  12:51:15.602  Complete is not a valid member of "Enum.PathStatus"  -  Server - MainScript:313
  12:51:15.602  Stack Begin  -  Studio
  12:51:15.602  Script 'ServerScriptService.AiService.MainScript', Line 313 - function startWandering  -  Studio - MainScript:313
  12:51:15.602  Script 'ServerScriptService.AiService.MainScript', Line 370  -  Studio - MainScript:370
  12:51:15.602  Stack End  -  Studio
  12:51:17.814  Disconnect from ::ffff:127.0.0.1|64438  -  Studio

i type “;trigger night” it only triggers then it plays the itsnight function i made i do it again and it plays the transition minus parts of it.

1 Like