An issue with getfenv()

Here’s the code Roblox provides.

-- Get the environment based on stack

local function whatIsThePassword(password)
    local env = getfenv(1)  -- Get the environment 1 level up, or whatever called this function
    print(env.password)  -- Get the password from the environment one level up
    print(password)  --> nil
end

local function openSesame()
    local password = "secret"  -- A variable local to openSesame()
    whatIsThePassword(password)
end

openSesame()

So, running this, the expected output would be

-- secret
-- nil

However, the actual output:

-- nil
-- secret

Anyone knows why? Tested in studio and in test.

The documentation is wrong. password in openSesame is local and won’t be in env.password, so the results are flopped.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.