I'm letting players write their own scripts (this being the basis of the game, so its absolutely required). They are loaded in with InsertService and all required by a central script. I made a table (baselua) which has some default functions and some custom functions. I use this to require the scripts:

local sandbox = function(module)
	return setfenv(require, baselua)(module)
end

This doesn't seem to work though because I apparently cant set the environment of "required".

'setfenv' cannot change environment of given object

I've tried:

local sandbox = function(module)
	local require = require
	return setfenv(function()
		require(module)
	end, baselua)
end

But that doesn't work ether, as it just doesn't carry the environment to the module script.

So I'm guessing I'll have to use some other method, I cant think of any safe alternatives though.

I cant trust people to set their function environment at the start of their script, and I cant access script Source to force it there.

#What should I do?

1 comment

0

You are unable to change module's enviroment when requiring it. It is possible if each of the modules would have init function which you would be able to setfenv(2, ...) through however if i understand you correctly it is not possible in your case.