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

