Untitled

mail@pastecode.io avatar
unknown
lua
a month ago
9.8 kB
4
Indexable
Never
------- SERVER
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local WeatherDirect = ReplicatedStorage.WeatherController

local ClientReplicator = WeatherDirect.RemoteEvents.ClientReplicator
local PauseWeatherEvent = WeatherDirect.RemoteEvents.PauseWeather
local RequestData = WeatherDirect.RemFunctions.RequestData


local CurrentChance = 1

local lowerChance = 1000


local GlobalWeatherData = {
	Time = math.random(7, 17),
	GameBright = 0,
	GameFogDensity = 0,
	CurrentWeather = 0,
	GlobalHaze = 0
}


local function calculateBrightness(time)
	return math.clamp(0.2 + 1.8 * (0.5 + 0.5 * math.sin(math.pi * time / 12)), 0.2, 2)
end

local function calculateFogDensity(time)
	return math.clamp(0.55 - 0.34 * (0.5 - 0.5 * math.sin(math.pi * time / 12)), 0.36, 0.55)

end

local function updateWeatherData()
	GlobalWeatherData.GameBright = calculateBrightness(GlobalWeatherData.Time)
	if GlobalWeatherData.CurrentWeather == 0 then
		GlobalWeatherData.GameFogDensity = calculateFogDensity(GlobalWeatherData.Time)
	end
end

local function PauseWeather()
	local ReturnTable = GlobalWeatherData
	
	local WeatherActionTable = {
		[1] = function()
			local finalTarg = math.random() * (0.700 - 0.900) + 0.900
			GlobalWeatherData.GameFogDensity = 0.400
			wait(1)
			GlobalWeatherData.GameFogDensity = 0.600
			
			GlobalWeatherData.GameFogDensity = finalTarg
		end,
		[2] = function()
			return
		end,
		[3] = function()
			GlobalWeatherData.GameFogDensity = 0.700
			GlobalWeatherData.GlobalHaze = 10
		end,
		[4] = function()
			GlobalWeatherData.GameFogDensity = 0.850
		end
		
	}
	
	local action = WeatherActionTable[GlobalWeatherData.CurrentWeather]
	action()
	
	wait(math.random(5, 15) * 60)
	GlobalWeatherData.CurrentWeather = 0
	GlobalWeatherData.GameFogDensity = ReturnTable.GameFogDensity
	GlobalWeatherData.GlobalHaze = ReturnTable.GlobalHaze
end

while true do
	wait(0.1)

	GlobalWeatherData.Time = (GlobalWeatherData.Time + 0.001) % 24

	updateWeatherData()

	if math.random(1, CurrentChance) > lowerChance and GlobalWeatherData.CurrentWeather == 0 and math.random(1, 100) >= 20 and math.random(1, 100) >= 21 then
		GlobalWeatherData.CurrentWeather = math.random(1, 4)
		local NewCo = coroutine.create(PauseWeather)

		coroutine.resume(NewCo)
		
		lowerChance = 1000
		CurrentChance = 1
	else
		CurrentChance = CurrentChance+1
		lowerChance = lowerChance-1
	end


	ClientReplicator:FireAllClients(GlobalWeatherData)

end

----------- CLIENT

local RainModule = require(script.Parent.RainScript.Rain)
local BlizzardModule = require(script.Parent.SnowScript.Rain)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local player = game.Players.LocalPlayer
local HTTPServ = game:GetService("HttpService")

local WeatherDirect = ReplicatedStorage.WeatherController
local ClientReplicator = WeatherDirect.RemoteEvents.ClientReplicator
local PauseWeather = WeatherDirect.RemoteEvents.PauseWeather
local RequestData = WeatherDirect.RemFunctions.RequestData

local IsRain = false
local IsBlizzard = false
local IsRad = false

local DoingThunder = false

local ActiveThunder = false

local DoingWeather = false


local SoundTable = {
	[1] = Enum.ReverbType.SewerPipe,
	[2] = Enum.ReverbType.GenericReverb
}

local tweening = false

local function tweenProperty(instance, property, targetValue)
	if tweening then
		return
	end

	local currentValue = instance[property]
	local difference = math.abs(currentValue - targetValue)

	if difference < 0.1 then
		instance[property] = targetValue
		return
	end

	tweening = true
	local tweenTime = math.clamp(difference * 0.5, 0.5, 5)
	local tweenInfo = TweenInfo.new(tweenTime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
	local tween = TweenService:Create(instance, tweenInfo, {[property] = targetValue})

	tween:Play()
	tween.Completed:Connect(function()
		tweening = false
	end)
end

RainModule:Disable(TweenInfo.new(0, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut))
BlizzardModule:Disable(TweenInfo.new(0, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut))

ClientReplicator.OnClientEvent:Connect(function(Package)
	local PlayerAt = player:GetAttribute("Type")
	PlayerAt = HTTPServ:JSONDecode(PlayerAt)

	if PlayerAt.Active ~= true then
		if DoingThunder ~= true then
			tweenProperty(game.Lighting, "Brightness", Package.GameBright)
		end
		tweenProperty(game.Lighting.Atmosphere, "Density", Package.GameFogDensity)
		tweenProperty(game.Lighting.Atmosphere, "Haze", Package.GlobalHaze)
		tweenProperty(game.Lighting, "ClockTime", Package.Time)
		game.SoundService.AmbientReverb = Enum.ReverbType.NoReverb
	else
		local function tweenClockTime(currentTime, targetTime)
			local forwardDistance = (targetTime - currentTime) % 24
			local backwardDistance = (currentTime - targetTime) % 24

			if forwardDistance <= backwardDistance then
				tweenProperty(game.Lighting, "ClockTime", targetTime) 
			else
				tweenProperty(game.Lighting, "ClockTime", targetTime, true)
			end
		end

		local function setClockTime()
			local targetTime = PlayerAt.AverageTime ~= "Default" and PlayerAt.AverageTime or Package.Time
			local currentTime = game.Lighting.ClockTime
			tweenClockTime(currentTime, targetTime)
		end

		local function setBrightness()
			local targetBrightness = PlayerAt.AverageDarkness ~= "Default" and PlayerAt.AverageDarkness or Package.GameBright
			if DoingThunder ~= true then
				tweenProperty(game.Lighting, "Brightness", targetBrightness)
			end
		end

		local function setAtmosphereDensity()
			local targetDensity = PlayerAt.AverageDensity ~= "Default" and PlayerAt.AverageDensity or Package.GameFogDensity
			tweenProperty(game.Lighting.Atmosphere, "Density", targetDensity)
		end
		
		

		local co1 = coroutine.create(setClockTime)
		local co2 = coroutine.create(setBrightness)
		local co3 = coroutine.create(setAtmosphereDensity)

		coroutine.resume(co1)
		coroutine.resume(co2)
		coroutine.resume(co3)
		game.Lighting.Atmosphere.Haze = 0

		game.SoundService.AmbientReverb = SoundTable[PlayerAt.SoundType]
	end
	
	if PlayerAt.Active ~= true then
		if DoingWeather ~= true and Package.CurrentWeather ~= 0 then
			DoingWeather = true
			local function ThunderSim()
				if ActiveThunder ~= true then
					ActiveThunder = true
					while true do
						wait(math.random(10,20))
						if IsRain or IsRad or IsBlizzard then
							local PlayerAt = player:GetAttribute("Type")
							PlayerAt = HTTPServ:JSONDecode(PlayerAt)
							if PlayerAt.Active ~= true then
								if math.random(1, 15) >= 2 then
									local thunderSound = game.Workspace.Sounds.Thunder
									thunderSound:Play()
									DoingThunder = true
									for I = 1, 3 do
										game.Lighting.Brightness = 10
										wait(0.01)
										game.Lighting.Brightness = Package.GameBright
										wait(0.01)
										game.Lighting.Brightness = 10
									end
									game.Lighting.Brightness = Package.GameBright
									DoingThunder = false
								else
									ActiveThunder = false
									break
								end
							else
								ActiveThunder = false
								break
							end
						else
							ActiveThunder = false
							break
						end
					end
				end
			end

			if Package.CurrentWeather == 2 then
				local rainSound = game.Workspace.Sounds.Rain
				rainSound.Volume = 0
				rainSound:Play()
				game:GetService("TweenService"):Create(rainSound, TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Volume = 0.5}):Play()
				IsRain = true
				RainModule:Enable(TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut))
				local co1 = coroutine.create(ThunderSim)
				coroutine.resume(co1)
			end

			if Package.CurrentWeather == 3 then
				local stormSound = game.Workspace.Sounds.Storm
				stormSound.Volume = 0
				stormSound:Play()
				game:GetService("TweenService"):Create(stormSound, TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Volume = 0.5}):Play()
				IsRad = true
				local co1 = coroutine.create(ThunderSim)
				coroutine.resume(co1)
			end

			if Package.CurrentWeather == 4 then
				local stormSound = game.Workspace.Sounds.Storm
				stormSound.Volume = 0
				stormSound:Play()
				game:GetService("TweenService"):Create(stormSound, TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Volume = 0.5}):Play()
				IsBlizzard = true
				BlizzardModule:Enable(TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut))
			end

		end
	end

	if DoingWeather == true and (Package.CurrentWeather == 0 or PlayerAt.Active == true) then
		local tweenService = game:GetService("TweenService")

		if IsRain then
			local rainSound = game.Workspace.Sounds.Rain
			local rainTween = tweenService:Create(rainSound, TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Volume = 0})
			rainTween:Play()
			rainTween.Completed:Connect(function()
				rainSound:Stop()
			end)
		end

		if IsRad then
			local stormSound = game.Workspace.Sounds.Storm
			local stormTween = tweenService:Create(stormSound, TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Volume = 0})
			stormTween:Play()
			stormTween.Completed:Connect(function()
				stormSound:Stop()
			end)
		end

		if IsBlizzard then
			local stormSound = game.Workspace.Sounds.Storm
			local stormTween = tweenService:Create(stormSound, TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Volume = 0})
			stormTween:Play()
			stormTween.Completed:Connect(function()
				stormSound:Stop()
			end)
		end

		if RainModule then RainModule:Disable(TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)) end
		if BlizzardModule then BlizzardModule:Disable(TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)) end

		DoingWeather = false
	end

end)

Leave a Comment