local G={}do local v local d local l local C local N local O local f={}local B={Backpack=true;Chat=true,Health=true;PlayerList=true}local T={BadgesNotificationsActive=true;PointsNotificationsActive=true}function G.Push()for G in pairs(B)do B[G]=StarterGui:GetCoreGuiEnabled(Enum.CoreGuiType[G])StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType[G],false)end for G in pairs(T)do T[G]=StarterGui:GetCore(G)StarterGui:SetCore(G,false)end local G=LocalPlayer:FindFirstChildOfClass("\080\108\097\121\101\114\071\117\105")if G then for G,v in pairs(G:GetChildren())do if v:IsA("\083\099\114\101\101\110\071\117\105")and v.Enabled then f[#f+1]=v v.Enabled=false end end end O=Camera.FieldOfView Camera.FieldOfView=70 l=Camera.CameraType Camera.CameraType=Enum.CameraType.Custom N=Camera.CFrame C=Camera.Focus d=UserInputService.MouseIconEnabled UserInputService.MouseIconEnabled=false v=UserInputService.MouseBehavior UserInputService.MouseBehavior=Enum.MouseBehavior.Default end function G.Pop()for G,v in pairs(B)do StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType[G],v)end for G,v in pairs(T)do StarterGui:SetCore(G,v)end for G,v in pairs(f)do if v.Parent then v.Enabled=true end end Camera.FieldOfView=O O=nil Camera.CameraType=l l=nil Camera.CFrame=N N=nil Camera.Focus=C C=nil UserInputService.MouseIconEnabled=d d=nil UserInputService.MouseBehavior=v v=nil end end local function v()local v=Camera.CFrame cameraRot=Vector2.new(v:toEulerAnglesYXZ())cameraPos=v.p cameraFov=Camera.FieldOfView velSpring:Reset(Vector3.new())panSpring:Reset(Vector2.new())fovSpring:Reset(0)G.Push()RunService:BindToRenderStep("\070\114\101\101\099\097\109",Enum.RenderPriority.Camera.Value,StepFreecam)Input.StartCapture()end local function d()Input.StopCapture()RunService:UnbindFromRenderStep("\070\114\101\101\099\097\109")G.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 = false

		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
