Can luacode be used in the preamble of a standalone-type document?

When standalone extracts the subpreamble, it drops all newlines, such that the preamble is saved as a single line. This not only breaks the lua code (for example "then" and "return" become "thenreturn"), it also confuses luacode which requires \end{luacode*} to be on a line of it's own.

One solution would be to move the luacode* block out of the preamble:

\documentclass{standalone}
\RequirePackage{luatex85}
\usepackage{pgfplots}
\usepackage{luacode}

\pgfmathdeclarefunction{Myfunc}{1}{%
  \edef\pgfmathresult{%
     \directlua{tex.print("" .. myfunc(#1))}%
  }%  
}

\begin{document}

%the lua code:
\begin{luacode*}
    function myfunc(x)
        if x==0 then
            return 0
        else
        x=3-(x-math.sqrt(3))^2
            return x
        end    
    end

function cobweb(x,n) --x is the starting point, n is the number of iterations
    y1=0
    for t=1,n,1 do
            y2=myfunc(x)
            tex.sprint("\\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
            y1=myfunc(x)
            tex.sprint("\\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
            x=y2
    end
end
\end{luacode*}
\begin{tikzpicture}[
    x                = 1cm,
    scale            = 2,
    myfunc/.style    = {domain = 0:5, samples = 100}
  ]
\draw[->,thick] (0,0)--(5,0) node[right]{$x$};
\draw[->,thick] (0,0)--(0,5) node[above]{$y$};
\clip (0,0) rectangle (5,5);
\color{red}\directlua{cobweb(.01,10)}
\draw[thick, blue] (0,0)--(5,5);
\draw[black, thick] plot [myfunc] (\x, {Myfunc(\x)});
\end{tikzpicture}
\end{document}

The allternative way would be to rewrite the lua block to work without newlines and using \directlua: Ass an explicit space at the end of every line, escape \\ using \string and use TeX instead of Lua comments.

\documentclass{standalone}
\RequirePackage{luatex85}
\usepackage{pgfplots}
\usepackage{luacode}

%the lua code:
\directlua{
    function myfunc(x) %
        if x==0 then %
            return 0 %
        else %
        x=3-(x-math.sqrt(3))^2 %
            return x %
            end %
    end %

function cobweb(x,n) %x is the starting point, n is the number of iterations
    y1=0 %
    for t=1,n,1 do %
            y2=myfunc(x) %
            tex.sprint("\string\\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);") %
            y1=myfunc(x) %
            tex.sprint("\string\\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);") %
            x=y2 %
    end %
end
}

\pgfmathdeclarefunction{Myfunc}{1}{%
  \edef\pgfmathresult{%
     \directlua{tex.print("" .. myfunc(#1))}%
  }%  
}

\begin{document}
\begin{tikzpicture}[
    x                = 1cm,
    scale            = 2,
    myfunc/.style    = {domain = 0:5, samples = 100}
  ]
\draw[->,thick] (0,0)--(5,0) node[right]{$x$};
\draw[->,thick] (0,0)--(0,5) node[above]{$y$};
\clip (0,0) rectangle (5,5);
\color{red}\directlua{cobweb(.01,10)}
\draw[thick, blue] (0,0)--(5,5);
\draw[black, thick] plot [myfunc] (\x, {Myfunc(\x)});
\end{tikzpicture}
\end{document}

put it before the \documentclass:

\RequirePackage{luacode}
\begin{luacode*}
function myfunc(x)
    if x==0 then
    return 0
    else
    x=3-(x-math.sqrt(3))^2
    return x
    end    
end
function cobweb(x,n) --x is the starting point, n is the number of iterations
    y1=0
    for t=1,n,1 do
    y2=myfunc(x)
    tex.sprint("\\draw[very thin, color=red, smooth] ("..x.."cm,"..y1.."cm)-- " .. "("..x.."cm,"..y2.."cm);")
    y1=myfunc(x)
    tex.sprint("\\draw[very thin, color=red, smooth] ("..x.."cm,"..y2.."cm)-- " .. "("..y2.."cm,"..y2.."cm);")
    x=y2
    end
end
\end{luacode*}
\documentclass[tikz]{standalone}
\usepackage{pgfplots}

\pgfmathdeclarefunction{Myfunc}{1}{%
    \edef\pgfmathresult{%
        \directlua{tex.print("" .. myfunc(#1))}%
    }%  
}
\begin{document}
\begin{tikzpicture}[
    x                = 1cm,
    scale            = 2,
    myfunc/.style    = {domain = 0:5, samples = 100}
    ]
    \draw[->,thick] (0,0)--(5,0) node[right]{$x$};
    \draw[->,thick] (0,0)--(0,5) node[above]{$y$};
    \clip (0,0) rectangle (5,5);
    \color{red}\directlua{cobweb(.01,10)}
    \draw[thick, blue] (0,0)--(5,5);
    \draw[black, thick] plot [myfunc] (\x, {Myfunc(\x)});
\end{tikzpicture}
\end{document}