Scoping in nested functions - Why am I not getting an error?

Also note that one doesn’t have to assign a value to the variable in the parent function, one can just declare it via local:

function baz()
    local x
    function bar()
        x = 2
    end
    bar()
    x
end

baz() # returns 2