Recently I’ve came up with a concept of a game where you need to write Lua code to perform tasks, the problem is that I have no idea on how to execute plain lua code without any stuff such as game,workspace, Usersettings, etc. and add an ability to require modules that will perform LuaU tasks (doing something with instances, editing values, etc.)
Could anyone explain how to do that?
Thanks.
Maybe you could use setfenv. Of course be careful running any code from a user on the server. Here’s an example with error checking. One problem will be any object parented to the workspace gives control of the entire workspace, maybe search the loadstring
for .Parent
and reject anything trying to use the supplied object’s parent.
--!strict
local user_function = loadstring("print(object.Name)")
if user_function then
local env_user_function = setfenv(user_function, {
print = print,
object = workspace.Baseplate
})
if env_user_function then
env_user_function()
else
warn("Couldn't set environment!")
end
else
warn("Couldn't load user function string!")
end
It doesn’t work for me. For 1 hour i’ve been trying to figure out how to use loadstring and it seems like this entire time i knew and have been trying to do that but it just doesn’t work and gives error. Why does it work for other but not for me? (loadstrings are enabled in ServerScriptServ)
It gives me error because variable is nil when i gived it the variable with env