local U=game.Lighting.ExposureCompensation local f local j local M local n local o local H local y local V function removehelmet()if plr.Character then if H then H:Disconnect()end animating=false togglenvg(false)actionservice:UnbindAction("\110\118\103\116\111\103\103\108\101")if M then M:Destroy()end if V then V:Destroy()end end end function oncharadded(u)(u:WaitForChild("\072\117\109\097\110\111\105\100")).Died:connect(function()removehelmet()end)u.ChildAdded:connect(function(u)local W if u.Name=="\072\101\108\109\101\116"then V=u M=Instance.new("\083\099\114\101\101\110\071\117\105")M.IgnoreGuiInset=true W=Instance.new("\084\101\120\116\066\117\116\116\111\110")W.Text="\072\101\108\109\101\116"W.Size=UDim2.new(.05,0,.035,0)W.TextColor3=Color3.new(.75,.75,.75)W.Position=UDim2.new(.1,0,.3,0)W.BackgroundTransparency=1 W.BackgroundColor3=Color3.fromRGB(124,52,38)W.Font=Enum.Font.SourceSansBold W.TextScaled=true W.TextTransparency=1 W.MouseButton1Down:connect(removehelmet)W.Parent=M M.Parent=plr.PlayerGui H=u.AncestryChanged:Connect(function(U,f)if not f then removehelmet()end end)end local l=u:WaitForChild("\085\112",.5)if l then f=l o=require(f:WaitForChild("\065\085\084\079\095\067\079\078\070\073\071"))y=f:WaitForChild("\078\086\071\095\083\101\116\116\105\110\103\115")local H=Instance.new("\073\109\097\103\101\076\097\098\101\108")H.BackgroundTransparency=1 H.ImageTransparency=1 local V=H:Clone()V.Image="\114\098\120\097\115\115\101\116\105\100\058\047\047"..y.OverlayImage.Value V.Size=UDim2.new(1,0,1,0)V.Name="\079\118\101\114\108\097\121"V.ZIndex=2 local u=y.RemoveButtonPosition.Value W.Position=UDim2.new(u.X,0,u.Y,0)H.Name="\078\111\105\115\101"H.AnchorPoint=Vector2.new(.5,.5)H.Position=UDim2.new(.5,0,.5,0)H.Size=UDim2.new(2,0,2,0)V.Parent=M H.Parent=M local i=o.tweeninfo local function b(U,f)if f then for f,j in pairs(f)do table.insert(U,j)end end end j=o.onanim n=o.offanim on_overlayanim={tweenservice:Create(game.Lighting,i,{ExposureCompensation=y.Exposure.Value});tweenservice:Create(colorcorrection,i,{Brightness=y.OverlayBrightness.Value;Contrast=.8;Saturation=-1;TintColor=y.OverlayColor.Value});tweenservice:Create(M.Overlay,i,{ImageTransparency=0});tweenservice:Create(M.Noise,i,{ImageTransparency=0})}off_overlayanim={tweenservice:Create(game.Lighting,i,{ExposureCompensation=U});tweenservice:Create(colorcorrection,i,{Brightness=0,Contrast=0;Saturation=0,TintColor=Color3.fromRGB(255,255,255)});tweenservice:Create(M.Overlay,i,{ImageTransparency=1}),tweenservice:Create(M.Noise,i,{ImageTransparency=1})}actionservice:BindAction("\110\118\103\116\111\103\103\108\101",function()togglenvg(not nvgactive)return Enum.ContextActionResult.Pass end,true,Enum.KeyCode.N)end end)end plr.CharacterAdded:connect(oncharadded)local u=workspace:FindFirstChild(plr.Name)if u then oncharadded(u)end function playtween(U)spawn(function()for U,f in pairs(U)do if typeof(f)=="\110\117\109\098\101\114"then wait(f)else f:Play()end end end)end function applyprops(U,f)for f,j in pairs(f)do U[f]=j end end function cycle(U)local f=M.Noise local j=U.src local n repeat n=j[math.random(1,#j)]until n~=U.last f.Image="\114\098\120\097\115\115\101\116\105\100\058\047\047"..n local o=math.random(230,255)f.Position=UDim2.new(math.random(.4,.6),0,math.random(.4,.6),0)f.ImageColor3=Color3.fromRGB(o,o,o)U.last=n end function togglenvg(U)if not animating and f then nvgevent:FireServer()M.TextButton.Visible=not U animating=true nvgactive=U if o.lens then o.lens.Material=U and"\078\101\111\110"or"\071\108\097\115\115"end if U then playtween(j)delay(.75,function()playtween(on_overlayanim)spawn(function()while nvgactive do cycle(o.dark)cycle(o.light)wait(.05)end end)animating=false end)else playtween(n)delay(.5,function()playtween(off_overlayanim)animating=false end)end end end nvgevent.OnClientEvent:connect(function(U,f)local j=U:WaitForChild("\116\119\105\115\116\106\111\105\110\116")local M=require(U.AUTO_CONFIG)local n=M.lens if n then n.Material=f and"\078\101\111\110"or"\071\108\097\115\115"end playtween(M[f and"\111\110\097\110\105\109"or"\111\102\102\097\110\105\109"])end)local W=game.Lighting local l=game.ReplicatedStorage local i=l:WaitForChild("\069\110\097\098\108\101\065\117\116\111\076\105\103\104\116\105\110\103")if i.Value then function llerp(U,f,j)return U*(1-j)+f*j end local U=(l:WaitForChild("\077\105\110\066\114\105\103\104\116\110\101\115\115")).Value local f=(l:WaitForChild("\077\097\120\066\114\105\103\104\116\110\101\115\115")).Value local j=(l:WaitForChild("\077\105\110\065\109\098\105\101\110\116")).Value local M=(l:WaitForChild("\077\097\120\065\109\098\105\101\110\116")).Value local n=(l:WaitForChild("\077\105\110\079\117\116\100\111\111\114\065\109\098\105\101\110\116")).Value local o=(l:WaitForChild("\077\097\120\079\117\116\100\111\111\114\065\109\098\105\101\110\116")).Value function setdaytime()local H=W.ClockTime local y=math.abs(H-12)local V=1-y/12 W.Brightness=llerp(U,f,V)W.Ambient=j:lerp(M,V)W.OutdoorAmbient=n:lerp(o,V)end;(game:GetService("\082\117\110\083\101\114\118\105\099\101")).RenderStepped:connect(setdaytime)end


local defexposure = game.Lighting.ExposureCompensation

local nvg
local onanim
local gui
local offanim
local config
local onremoved
local setting
local helmet

function removehelmet()
	if plr.Character then
		if onremoved then
			onremoved:Disconnect()
		end
		animating = false
		togglenvg(false)
		actionservice:UnbindAction("nvgtoggle")
		if gui then
			gui:Destroy()
		end
		if helmet then
			helmet:Destroy()
		end
	end
end

function oncharadded(newchar)
	newchar:WaitForChild("Humanoid").Died:connect(function()
		removehelmet()
	end)
	newchar.ChildAdded:connect(function(child)
		local removebutton
		if child.Name == "Helmet" then
			helmet = child
			gui = Instance.new("ScreenGui")
			gui.IgnoreGuiInset = true

			removebutton = Instance.new("TextButton")
			removebutton.Text = "Helmet"
			removebutton.Size = UDim2.new(.05,0,.035,0)
			removebutton.TextColor3 = Color3.new(.75,.75,.75)
			removebutton.Position = UDim2.new(.1,0,.3,0)
			removebutton.BackgroundTransparency = 1
			removebutton.BackgroundColor3 = Color3.fromRGB(124, 52, 38)
			removebutton.Font = Enum.Font.SourceSansBold
			removebutton.TextScaled = true
			removebutton.TextTransparency = 1
			removebutton.MouseButton1Down:connect(removehelmet)

			removebutton.Parent = gui
			gui.Parent = plr.PlayerGui

			onremoved = child.AncestryChanged:Connect(function(_, parent)
				if not parent then
					removehelmet()
				end
			end)

		end
		local newnvg = child:WaitForChild("Up",.5)
		if newnvg then
			nvg = newnvg
			config = require(nvg:WaitForChild("AUTO_CONFIG"))
			setting = nvg:WaitForChild("NVG_Settings")


			local noise = Instance.new("ImageLabel")
			noise.BackgroundTransparency = 1
			noise.ImageTransparency = 1

			local overlay = noise:Clone()
			overlay.Image = "rbxassetid://"..setting.OverlayImage.Value
			overlay.Size = UDim2.new(1,0,1,0)
			overlay.Name = "Overlay"
			overlay.ZIndex = 2

			local buttonpos = setting.RemoveButtonPosition.Value
			removebutton.Position = UDim2.new(buttonpos.X,0,buttonpos.Y,0)

			noise.Name = "Noise"
			noise.AnchorPoint = Vector2.new(.5,.5)
			noise.Position = UDim2.new(.5,0,.5,0)
			noise.Size = UDim2.new(2,0,2,0)


			overlay.Parent = gui
			noise.Parent = gui

			local info = config.tweeninfo

			local function addtweens(base,extra)
				if extra then
					for _,tween in pairs(extra)do
						table.insert(base,tween)
					end
				end
			end

			onanim = config.onanim
			offanim = config.offanim

			on_overlayanim = {
				tweenservice:Create(game.Lighting,info,{ExposureCompensation = setting.Exposure.Value}),
				tweenservice:Create(colorcorrection,info,{Brightness = setting.OverlayBrightness.Value,Contrast = .8,Saturation = -1,TintColor = setting.OverlayColor.Value}),
				tweenservice:Create(gui.Overlay,info,{ImageTransparency = 0}),
				tweenservice:Create(gui.Noise,info,{ImageTransparency = 0}),
			}


			off_overlayanim = {
				tweenservice:Create(game.Lighting,info,{ExposureCompensation = defexposure}),
				tweenservice:Create(colorcorrection,info,{Brightness = 0,Contrast = 0,Saturation = 0,TintColor = Color3.fromRGB(255, 255, 255)}),
				tweenservice:Create(gui.Overlay,info,{ImageTransparency = 1}),
				tweenservice:Create(gui.Noise,info,{ImageTransparency = 1}),
			}


			actionservice:BindAction("nvgtoggle",function() togglenvg(not nvgactive) return Enum.ContextActionResult.Pass end, true, Enum.KeyCode.N)

		end
	end)
end

plr.CharacterAdded:connect(oncharadded)

local oldchar = workspace:FindFirstChild(plr.Name)
if oldchar then
	oncharadded(oldchar)
end


function playtween(tweentbl)
	spawn(function()
		for _,step in pairs(tweentbl) do
			if typeof(step) == "number" then
				wait(step)
			else
				step:Play()
			end
		end
	end)
end

function applyprops(obj,props)
	for propname,val in pairs(props)do
		obj[propname] = val
	end
end



function cycle(grain)
	local label = gui.Noise
	local source = grain.src
	local newframe
	repeat newframe = source[math.random(1, #source)]; 
	until newframe ~= grain.last 
	label.Image = 'rbxassetid://'..newframe
	local rand = math.random(230,255)
	label.Position = UDim2.new(math.random(.4,.6),0,math.random(.4,.6),0)
	label.ImageColor3 = Color3.fromRGB(rand,rand,rand)
	grain.last = newframe
end


function togglenvg(bool)
	if not animating and nvg then
		nvgevent:FireServer()
		gui.TextButton.Visible = not bool
		animating = true
		nvgactive = bool
		if config.lens then
			config.lens.Material = bool and "Neon" or "Glass"
		end
		if bool then
			playtween(onanim)
			delay(.75,function()
				playtween(on_overlayanim)
				spawn(function()
					while nvgactive do 
						cycle(config.dark)
						cycle(config.light)
						wait(0.05)
					end
				end)
				animating = false
			end)
		else
			playtween(offanim)
			delay(.5,function()
				playtween(off_overlayanim)
				animating = false
			end)
		end
	end	
end


nvgevent.OnClientEvent:connect(function(nvg,activate)
	local twistjoint = nvg:WaitForChild("twistjoint")
	local config = require(nvg.AUTO_CONFIG)
	local lens = config.lens
	if lens then
		lens.Material = activate and "Neon" or "Glass"
	end
	playtween(config[activate and "onanim" or "offanim"])
end)

local lighting = game.Lighting
local rs = game.ReplicatedStorage

local autolighting = rs:WaitForChild("EnableAutoLighting")

if autolighting.Value then

	function llerp(a,b,t)
		return a*(1-t)+b*t
	end

	local minbrightness = rs:WaitForChild("MinBrightness").Value
	local maxbrightness = rs:WaitForChild("MaxBrightness").Value
	local minambient = rs:WaitForChild("MinAmbient").Value
	local maxambient = rs:WaitForChild("MaxAmbient").Value
	local minoutdoor = rs:WaitForChild("MinOutdoorAmbient").Value
	local maxoutdoor = rs:WaitForChild("MaxOutdoorAmbient").Value

	function setdaytime()
		local newtime = lighting.ClockTime
		local middaydiff = math.abs(newtime-12)
		local f = (1-middaydiff/12)
		lighting.Brightness = llerp(minbrightness,maxbrightness,f)
		lighting.Ambient = minambient:lerp(maxambient,f)
		lighting.OutdoorAmbient = minoutdoor:lerp(maxoutdoor,f)
	end

	game:GetService("RunService").RenderStepped:connect(setdaytime)

end

