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

