I've been playing around with environment manipulation, and ran into something that really stumbled me. When you try to index a nil value in an environment or table, it should invoke the __index metamethod (if one is set to it). However in my case, it's not doing that. Here's my code:
function Test(x)
print(x) -- print's nil
end
setfenv(Test,setmetatable({},{
__index=function(t,k)
if(k=='x')then
print("No '"..k.."' value exists, returning default...")
return "Replaced with this string"
else
return getfenv()[k]
end
end
}))
Test() -- not assigning any parameters (so, "x" should be nil, therefore allowing "__index" to be invoked when the environment is indexed for "x", like in it's print function above).
Yet, the output still prints "nil". Can anyone help please? Is there any way I can use __index on this function without getting rid of it's argument?

3 comments
fenv metatables don't register for indexing a local variable. function arguments count as local.
Shows how much I know about function environments. Your comment does make a perfect answer though.
Still helped clear my miss-understanding. Thank you both.