✓ Accepted answer
Just a side note: table.foreach is deprecated, use a generic for loop instead.
setfenv
setfenv(f, env) sets a function's environment. When it receives a function as the first argument, it will set f's environment to env . The env table is a dictionary of what will be the variables for the functions environment.
Here is an example:
local function fn()
print("Fn")
end
setfenv(fn, { })
fn()
You will get an exception like attempt to call a nil value, because the environment is empty.
Now let's add print variable in the environment of fn.
setfenv(fn, { print = print })
fn()
When called with 1, it will set the environment of the current environment.
setfenv(1, { })
print("XD")
The same attempt to call a nil value exception.
You could use 2, which is another environment, however I am unsure of what it exactly is. I think there are numbers you could use too.
getfenv
getfenv(fn) is practically the opposite. It gets an environment. Knowing how setfenv works now, I think I don't need to explain further. The numbers thing is still the same.
1 comment
pls dont forget about me :((