Hi there, I am trying to output prints, warnings and errors into a table in my code editor I made. The issue is, is that I have used a getfenv, and setfenv to output of those within the sandbox environment, which throws an error saying invalid argument #1 to 'setfenv' (number expected, got table). I do not know what else to do as I am basically confused at this point since I returned the index in the metatable.
local outputlines = {}
local scriptran = loadstring(text::string)
local env = getfenv(scriptran)
local sandbox = setmetatable({}, {
__index = function(_,var)
return env[var]
end,
print = function(...)
table.insert(outputlines,(...))
end,
warn = function(...)
table.insert(outputlines,(...))
end,
error = function(...)
table.insert(outputlines,(...))
end,
})
setfenv(env,sandbox)
scriptran()
for i,v in ipairs(outputlines) do
print(v)
end
Not sure exactly what you’re trying to achieve, would you mind explaining? In the snipper above I simply switched the functionality of the global ‘print’ and ‘warn’ functions.
I have a custom code editor I made in my game, and along with that is output that is meant to receive prints, warnings and output just like the one in studio