Loading game menu when local function is called

So, I tried multiple ways of starting a game menu that should popup when the game is loaded or a key is pressed using a local function, but it doesn’t work neither times. (Please, note that every needed variable is already declared but not shown here.)
Is there any way to start the while loop with a function?

local function OpenMenu()
	BoolLoop = true
	task.wait()
	PlayerCamera.CameraType = CameraModeOn
	PlayerCamera.FieldOfView = 110
	MenuGui:Clone(Player.PlayerGui)
	while BoolLoop do
		PlayerCamera.CFrame = CamPart.CFrame
		task.wait()
		if not BoolLoop then
			break
		end
	end
end

local function CloseMenu()
	BoolLoop = false
	task.wait()
	PlayerCamera.CameraType = CameraModeOff
	PlayerCamera.FieldOfView = 70
	Player.PlayerGui.MenuGui:Destroy()
end

game.Loaded:Connect(OpenMenu)
ContextActionService:BindAction("OpenMenu", OpenMenu, true, "m")