There are no errors, I can't figure out what's wrong. Here's the code
--Print is only sandboxed for testing --Loadstring IS enabled and this IS a server sided script Blocked = {'print'} local NewEnv,MT = {},{} MT.__index = function(Table,Key) if Blocked[Key] then error('This function has been sandboxed') else return getfenv(1)[Key] end end setmetatable(NewEnv,MT) local Function = loadstring([[print('hello')]]) setfenv(Function,NewEnv) Function()
The problem is that you initialized Blocked as a list of commands to block, but in the __index metamethod, you're using it as if it were a dictionary. The dictionary will be faster if you have more than 1 or 2 blocked commands, so either initialize it like this:
Blocked = {print=true}
or convert it into a dictionary like this:
Blocked = {'print'} for i = 1, #Blocked do Blocked[Blocked[i]] = true Blocked[i] = nil --remove numerical index (it's no longer needed) end
(You technically don't have to set the value to 'true' -- any value other than nil and false will do.)
In terms of debugging, I recommend using print in key spots. ex, in this case, I found it useful to put a print statement after the "else" command to make sure that the function was being used (after all, if the function isn't even being called, then the error must lie outside the function). I used print("InMT", Table, Key)
, thereby allowing me to know what the values of table/key were. Once I confirmed that the function was getting called (since the print statement printed a couple lines), I looked more closely at what the function might be doing wrong by stepping through the code. Right on the first line is the error: Blocked["print"] was nil (even though Blocked[1] == "print").