This prints 5 and then 6. It's supposed to add 1 to any variable set, so a should be 6 and b should be 6, but __newindex doesn't affect a because it was declared locally. Is there any way to filter local variables?
do
local e={print=print}
setfenv(1,setmetatable({},{
__index=function(t,k)
return e[k]or loadstring('return '..k)()
end,
__newindex=function(t,k,v)
e[k]=v+1
end
}))
end
local a=5
print(a)
b=5
print(b)

2 comments
Isn't the debug library only Lua 5.2?
There are other ways to sandbox, (Obv), I have seen a few.. My friend is INSANE at sandboxing.. If you still need help ask LordRevorius and tell him I sent you.