local K={}do local l local c local F local e local w local G local p={}local H={Backpack=true;Chat=true;Health=true;PlayerList=true}local O={BadgesNotificationsActive=true,PointsNotificationsActive=true}function K.Push()G=Camera.FieldOfView Camera.FieldOfView=70 F=Camera.CameraType Camera.CameraType=Enum.CameraType.Custom w=Camera.CFrame e=Camera.Focus c=UserInputService.MouseIconEnabled UserInputService.MouseIconEnabled=true l=UserInputService.MouseBehavior UserInputService.MouseBehavior=Enum.MouseBehavior.Default end function K.Pop()for K,l in pairs(H)do StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType[K],l)end for K,l in pairs(O)do StarterGui:SetCore(K,l)end for K,l in pairs(p)do if l.Parent then l.Enabled=true end end Camera.FieldOfView=G G=nil Camera.CameraType=F F=nil Camera.CFrame=w w=nil Camera.Focus=e e=nil UserInputService.MouseIconEnabled=c c=nil UserInputService.MouseBehavior=l l=nil end end local function l()local l=Camera.CFrame cameraRot=Vector2.new(l:toEulerAnglesYXZ())cameraPos=l.p cameraFov=Camera.FieldOfView velSpring:Reset(Vector3.new())panSpring:Reset(Vector2.new())fovSpring:Reset(0)K.Push()RunService:BindToRenderStep("\070\114\101\101\099\097\109",Enum.RenderPriority.Camera.Value,StepFreecam)Input.StartCapture()end local function c()Input.StopCapture()RunService:UnbindFromRenderStep("\070\114\101\101\099\097\109")K.Pop()end


local PlayerState = {} do
	local mouseBehavior
	local mouseIconEnabled
	local cameraType
	local cameraFocus
	local cameraCFrame
	local cameraFieldOfView
	local screenGuis = {}
	local coreGuis = {
		Backpack = true,
		Chat = true,
		Health = true,
		PlayerList = true,
	}
	local setCores = {
		BadgesNotificationsActive = true,
		PointsNotificationsActive = true,
	}

	-- Save state and set up for freecam
	function PlayerState.Push()
		--for name in pairs(coreGuis) do
		--	coreGuis[name] = StarterGui:GetCoreGuiEnabled(Enum.CoreGuiType[name])
		--	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType[name], false)
		--end
		--for name in pairs(setCores) do
		--	setCores[name] = StarterGui:GetCore(name)
		--	StarterGui:SetCore(name, false)
		--end
		--local playergui = LocalPlayer:FindFirstChildOfClass("PlayerGui")
		--if playergui then
		--	for _, gui in pairs(playergui:GetChildren()) do
		--		if gui:IsA("ScreenGui") and gui.Enabled then
		--			screenGuis[#screenGuis + 1] = gui
		--			gui.Enabled = false
		--		end
		--	end
		--end

		cameraFieldOfView = Camera.FieldOfView
		Camera.FieldOfView = 70

		cameraType = Camera.CameraType
		Camera.CameraType = Enum.CameraType.Custom

		cameraCFrame = Camera.CFrame
		cameraFocus = Camera.Focus

		mouseIconEnabled = UserInputService.MouseIconEnabled
		UserInputService.MouseIconEnabled = true

		mouseBehavior = UserInputService.MouseBehavior
		UserInputService.MouseBehavior = Enum.MouseBehavior.Default
	end

	-- Restore state
	function PlayerState.Pop()
		for name, isEnabled in pairs(coreGuis) do
			StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType[name], isEnabled)
		end
		for name, isEnabled in pairs(setCores) do
			StarterGui:SetCore(name, isEnabled)
		end
		for _, gui in pairs(screenGuis) do
			if gui.Parent then
				gui.Enabled = true
			end
		end

		Camera.FieldOfView = cameraFieldOfView
		cameraFieldOfView = nil

		Camera.CameraType = cameraType
		cameraType = nil

		Camera.CFrame = cameraCFrame
		cameraCFrame = nil

		Camera.Focus = cameraFocus
		cameraFocus = nil

		UserInputService.MouseIconEnabled = mouseIconEnabled
		mouseIconEnabled = nil

		UserInputService.MouseBehavior = mouseBehavior
		mouseBehavior = nil
	end
end

local function StartFreecam()
	local cameraCFrame = Camera.CFrame
	cameraRot = Vector2.new(cameraCFrame:toEulerAnglesYXZ())
	cameraPos = cameraCFrame.p
	cameraFov = Camera.FieldOfView

	velSpring:Reset(Vector3.new())
	panSpring:Reset(Vector2.new())
	fovSpring:Reset(0)

	PlayerState.Push()
	RunService:BindToRenderStep("Freecam", Enum.RenderPriority.Camera.Value, StepFreecam)
	Input.StartCapture()
end

local function StopFreecam()
	Input.StopCapture()
	RunService:UnbindFromRenderStep("Freecam")
	PlayerState.Pop()
end
