A Roblox Community
Would you like to react to this message? Create an account in a few clicks or log in to continue.

A Roblox Community

A community of Robloxians who want to learn to script and build on Roblox Studio.
 
HomeLatest imagesRegisterLog in
If you're a experienced coder make some tutorials! It would really help the site grow.
Make sure you read the rules(Which can be found by clicking here)
If you're a beginner at coding, try some tutorials.
We have many Moderators/Admins watching this site. Contact them with Questions.
Let us know what your favorite sport is. By clicking here to vote (Click here)
This site is becoming inactive. Lets make it active.
Log in
Username:
Password:
Log in automatically: 
:: I forgot my password
Top posters
blueymaddog
Tutorial ~ getfenv()/setfenv() Vote_lcapTutorial ~ getfenv()/setfenv() Voting_barTutorial ~ getfenv()/setfenv() Vote_rcap 
naknak
Tutorial ~ getfenv()/setfenv() Vote_lcapTutorial ~ getfenv()/setfenv() Voting_barTutorial ~ getfenv()/setfenv() Vote_rcap 
Supernapalm
Tutorial ~ getfenv()/setfenv() Vote_lcapTutorial ~ getfenv()/setfenv() Voting_barTutorial ~ getfenv()/setfenv() Vote_rcap 
m27frogy
Tutorial ~ getfenv()/setfenv() Vote_lcapTutorial ~ getfenv()/setfenv() Voting_barTutorial ~ getfenv()/setfenv() Vote_rcap 
slayer9365
Tutorial ~ getfenv()/setfenv() Vote_lcapTutorial ~ getfenv()/setfenv() Voting_barTutorial ~ getfenv()/setfenv() Vote_rcap 
myrco919
Tutorial ~ getfenv()/setfenv() Vote_lcapTutorial ~ getfenv()/setfenv() Voting_barTutorial ~ getfenv()/setfenv() Vote_rcap 
branefreez
Tutorial ~ getfenv()/setfenv() Vote_lcapTutorial ~ getfenv()/setfenv() Voting_barTutorial ~ getfenv()/setfenv() Vote_rcap 
ninga95
Tutorial ~ getfenv()/setfenv() Vote_lcapTutorial ~ getfenv()/setfenv() Voting_barTutorial ~ getfenv()/setfenv() Vote_rcap 
CloneTrooper787
Tutorial ~ getfenv()/setfenv() Vote_lcapTutorial ~ getfenv()/setfenv() Voting_barTutorial ~ getfenv()/setfenv() Vote_rcap 
raboy117
Tutorial ~ getfenv()/setfenv() Vote_lcapTutorial ~ getfenv()/setfenv() Voting_barTutorial ~ getfenv()/setfenv() Vote_rcap 
Top posting users this month
No user
Latest topics
» Where to go from here.
Tutorial ~ getfenv()/setfenv() EmptySeptember 14th 2020, 1:20 pm by MrNicNac

» Send me an Email
Tutorial ~ getfenv()/setfenv() EmptySeptember 14th 2020, 1:16 pm by MrNicNac

» [v1.6.0.0] Lua Script Obfuscator [No Bytecode]
Tutorial ~ getfenv()/setfenv() EmptyJuly 6th 2015, 7:38 pm by m27frogy

» New Site Possibly
Tutorial ~ getfenv()/setfenv() EmptyJuly 6th 2015, 4:16 pm by m27frogy

» Ambassador!
Tutorial ~ getfenv()/setfenv() EmptyApril 15th 2015, 11:40 pm by naknak

» Boop - Tag
Tutorial ~ getfenv()/setfenv() EmptyApril 13th 2015, 9:46 pm by naknak

» Vip Class Script
Tutorial ~ getfenv()/setfenv() EmptyApril 13th 2015, 4:54 pm by naknak

» Who's active?!
Tutorial ~ getfenv()/setfenv() EmptyApril 13th 2015, 4:52 pm by naknak

» Genesis Point
Tutorial ~ getfenv()/setfenv() EmptyJuly 17th 2014, 7:04 pm by branefreez

» Reward System
Tutorial ~ getfenv()/setfenv() EmptyJuly 17th 2014, 5:41 am by m27frogy

» Script Request
Tutorial ~ getfenv()/setfenv() EmptyJuly 10th 2014, 11:43 am by naknak

» local scripts?
Tutorial ~ getfenv()/setfenv() EmptyJuly 10th 2014, 11:39 am by naknak

» Project: Reconstruction [Died]
Tutorial ~ getfenv()/setfenv() EmptyJuly 10th 2014, 11:36 am by naknak

» Hi. I am new here
Tutorial ~ getfenv()/setfenv() EmptyApril 26th 2014, 4:01 pm by altshiftkey

» What's your favorite sport?
Tutorial ~ getfenv()/setfenv() EmptyJanuary 1st 2014, 2:13 pm by m27frogy

» FlashLight Script
Tutorial ~ getfenv()/setfenv() EmptyJanuary 1st 2014, 2:11 pm by m27frogy

» Gun Making! [READ DESC]
Tutorial ~ getfenv()/setfenv() EmptyJanuary 1st 2014, 2:10 pm by m27frogy

» Hi, I am new here!
Tutorial ~ getfenv()/setfenv() EmptyNovember 26th 2013, 3:33 pm by Keanu73

» Improve Coding
Tutorial ~ getfenv()/setfenv() EmptyOctober 26th 2013, 1:12 pm by pook03

» Simple Button
Tutorial ~ getfenv()/setfenv() EmptySeptember 1st 2013, 6:19 pm by branefreez


 

 Tutorial ~ getfenv()/setfenv()

Go down 
+2
myrco919
raboy117
6 posters
AuthorMessage
Guest
Guest
avatar



Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJanuary 23rd 2011, 11:49 pm

This is a tutorial about setfenv() and getfenv(). Since it's in the advanced section, you should understand functions, tables, variables, etc. Knowing what pointer variables are is good too, but since we don't have those in Lua, I'll cover those too.

Now, you may be wondering, "What do setfenv and getfenv mean? What the heck is a fenv?" Well, "fenv" is short for "function environment". Without overcomplicating things, the function environment is all the variables that you have access to in a certain spot.

variables and the function environment

The function environment is like a table, and when you set a variable, you're setting a new index (the variable) to a new value (the value it is being assigned to).

Code:
Pie = "Pie"

In the above example, you are setting a new index of Pie in the function environment, and giving it the value of "Pie".

setfenv()

setfenv is a function that you use to change the function environment to a new function environment. The first argument is where to change the function environment. You can give it an actual function to change the environment of, or you can tell it how many stack levels to go down. Zero would be telling it to manipulate the function environment that the setfenv() is inside, one would tell it to change the function environment of where the current function was called from, etc.

The second argument is a table that will become the new function environment. The indices you set are variables that can be accessed in the function, and the values are the values those variables are assigned to.

Code:
setfenv(1, {Stuff = "Hello, World!"})
print(Stuff)

Now, if you were to test the above code, it would give you output something like this:

Script:2:Attempt to call global 'print' (a nil value)

You may be wondering why that is. print is a global function, right? Well, when you use setfenv(), you completely reset the function environment. Global variables need to be re-defined.

Code:
setfenv(1, {Stuff = "Hello, World!", print = print})
print(Stuff) -->Hello, World!


getfenv()

getfenv is another function, but before I explain too much about it, I'm going to explain a little bit about pointer variables.

Pointer variables are variables that, instead of just having the same value as another variable, actually use the same data as another variable. For example:

Code:
a = 5
b = a
a = 6
print(a,b) -->6 5

See how b didn't changed? It's because b simply got a clone of the data that a contained, instead of actually having the same data. Pointer variables use the same exact data. Let's say that I could create a pointer variable by adding an asterisk after a variable. Here's how it would work:

Code:
Var1 = 5
Var2* = Var1 -- this is now a pointer variable :O
Var1 = 6
print(Var1, Var2) -->6 (numbers/letters)

But why did it print like that? That's where the data is stored. The pointer variable doesn't refer to the data itself, it refers to the spot where the data is. We will use the ampersand (&) to refer to the data itself.

Code:
Var1 = 5
Var2* = Var1
Var1 = 6
print(Var1, &Var2) -->6 6

See? And since pointers point the the space where the data is stored, they can change the data too by changing what is in that spot:

Code:
Var1 = 5
Var2* = Var1
Var2 = 6
print(Var1) -->6

See how when we edited Var2, Var1 changed too? That's the usefulness of a pointer variable. Sadly, we can't use any type of pointer variable in Lua, or RBX.Lua.

Now back to getfenv.

The reason I had to explain pointers to you is because getfenv returns something that acts like a pointer variable. When you use getfenv(), it returns a table of the function environment. It only takes one argument, and it acts just like a pointer variable, because when you manipulate it, you manipulate the function environment that it refers to.

Note: When using getfenv(), leaving the parenthesis empty is the same as putting a zero in them. It takes the function environment of the current function.

Code:
pie = getfenv()
pie.Caik = "Hello!"
print(Caik) -->Hello!

See? We added an index to the function environment that we were currently in, and then we were able to use it. A shorter way to do this is:

Code:
getfenv().Caik = "Hi!" -- no just shortening the word doesn't make it a shorter script
print(Caik) -->Hi!

And another example of each:

Code:
function Pie()
print(lolwut)
end

function Caik()
print(lolwut)
end

getfenv(Pie).lolwut = "Hola"
setfenv(Caik, {print = print, lolwut = "Bonjour"})
Pie() -->Hola
Caik() -->Bonjour

See? Simple as that.

FEEDBACK PL0X?
Back to top Go down
raboy117
Novice Scripter
Novice Scripter
raboy117


Posts : 75
Join date : 2011-01-19
Age : 27
Location : Canada, PEI, Charlottetown

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJanuary 23rd 2011, 11:51 pm

Nice!
Back to top Go down
myrco919
Intermediate Scripter
Intermediate Scripter
myrco919


Posts : 190
Join date : 2011-01-21
Age : 26
Location : Holland

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJanuary 24th 2011, 9:57 am

Great
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter
avatar


Posts : 393
Join date : 2011-01-17

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJanuary 24th 2011, 8:45 pm

he makes things look easy. lol
Back to top Go down
http://hackthissite.org
blueymaddog
Administrator
Administrator
blueymaddog


Posts : 1081
Join date : 2010-12-09
Age : 26

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJanuary 26th 2011, 8:48 am

Great tutorial. But would this work?

Script 1:
getfenv().hi = "lol"
Script 2:
print(hi)
>lol
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter
avatar


Posts : 393
Join date : 2011-01-17

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJanuary 27th 2011, 3:48 am

that might work... might

this is my question

Code:
setmetatable(tab = {}, {__metatable = "access denied"})
print(getfenv(getmetatable(tab).__metatable = nil))
--> no complaints?

would that work?
Back to top Go down
http://hackthissite.org
blueymaddog
Administrator
Administrator
blueymaddog


Posts : 1081
Join date : 2010-12-09
Age : 26

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJanuary 27th 2011, 8:10 am

it might.
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter
avatar


Posts : 393
Join date : 2011-01-17

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJanuary 27th 2011, 8:29 pm

wait no i meant this

Code:
print(getfenv(setmetatable(tab = {}, {__metatable = "access denied"}).__metatable = nil))

Back to top Go down
http://hackthissite.org
blueymaddog
Administrator
Administrator
blueymaddog


Posts : 1081
Join date : 2010-12-09
Age : 26

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJanuary 27th 2011, 9:00 pm

it won't work, you can't use more than one '=' in one line except for '=='.
the parameter for getfenv has to be a number, blank or function. not a table.
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter
avatar


Posts : 393
Join date : 2011-01-17

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJanuary 27th 2011, 9:18 pm

first of all, you can use more than one line in tables

Code:
tab = {a = 1, b = 2, c = 3}

and second of all, setmetatable() is a function
Back to top Go down
http://hackthissite.org
blueymaddog
Administrator
Administrator
blueymaddog


Posts : 1081
Join date : 2010-12-09
Age : 26

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJanuary 28th 2011, 4:48 am

Oh ya forgot about that. Try testing your script in the output.
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter
avatar


Posts : 393
Join date : 2011-01-17

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJanuary 28th 2011, 10:18 pm

thats my problem. wheres the output?
Back to top Go down
http://hackthissite.org
blueymaddog
Administrator
Administrator
blueymaddog


Posts : 1081
Join date : 2010-12-09
Age : 26

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJanuary 29th 2011, 6:41 am

view>output in roblox studio
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter
avatar


Posts : 393
Join date : 2011-01-17

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJanuary 29th 2011, 2:40 pm

k thx
Back to top Go down
http://hackthissite.org
ninga95
Administrator
Administrator
ninga95


Posts : 122
Join date : 2010-07-30
Age : 29

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyMarch 6th 2011, 11:13 pm

Heres an observation i made earlier about this for people who are knew to this ... To always remember what Setfenv() and Getfenv() are you remember it like this.

Set in Setfenv() is Set, f in Setfenv() is function, env in Setfenv() is Environment, and put it all together and it means Setfenv() Set's the function's environment,

Get in Getfenv() is Get, f in Getfenv is function, env in Getfenv() is environment, put it all together, and it means Getfenv() Get's a function's environment.

Nice little observation i made earlier today lol, but this is a good way to remember what Setfenv() and Getfenv() does.
Back to top Go down
blueymaddog
Administrator
Administrator
blueymaddog


Posts : 1081
Join date : 2010-12-09
Age : 26

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyMarch 20th 2011, 6:10 am

would this be a _G fixer:

script1:

getfenv(print).bai = "lol"

script2:
print(bai)
Back to top Go down
m27frogy
The Garbageman
m27frogy


Posts : 336
Join date : 2011-06-23

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJune 30th 2011, 11:53 am

Great job NotAPotato!
Quote :
Heres an observation i made earlier about this for people who are knew to this ... To always remember what Setfenv() and Getfenv() are you remember it like this.

Set in Setfenv() is Set, f in Setfenv() is function, env in Setfenv() is Environment, and put it all together and it means Setfenv() Set's the function's environment,

Get in Getfenv() is Get, f in Getfenv is function, env in Getfenv() is environment, put it all together, and it means Getfenv() Get's a function's environment.

Nice little observation i made earlier today lol, but this is a good way to remember what Setfenv() and Getfenv() does.
Not a bad way of looking at it ninga!
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter
avatar


Posts : 393
Join date : 2011-01-17

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJune 30th 2011, 5:20 pm

erm. thats why the creator of the lua gave this function its name

setfenv: set function environment
getfenv: get function environment
Back to top Go down
http://hackthissite.org
m27frogy
The Garbageman
m27frogy


Posts : 336
Join date : 2011-06-23

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJune 30th 2011, 5:46 pm

I knew that Supernapalm. That still doesn't effect my actual statement, "way of looking at it".
Back to top Go down
Supernapalm
Expert Scripter
Expert Scripter
avatar


Posts : 393
Join date : 2011-01-17

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJune 30th 2011, 5:51 pm

actaully, its pretty much the only way of looking at it, cuz the lua creator said so
Back to top Go down
http://hackthissite.org
m27frogy
The Garbageman
m27frogy


Posts : 336
Join date : 2011-06-23

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyJune 30th 2011, 6:20 pm

Yes, that way of looking at it is still interesting.
Back to top Go down
blueymaddog
Administrator
Administrator
blueymaddog


Posts : 1081
Join date : 2010-12-09
Age : 26

Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() EmptyFebruary 11th 2012, 7:54 am

lol it's the way I looped at it before i saw this tutorial. I think it's the only way of seeing it. :P
Back to top Go down
Sponsored content





Tutorial ~ getfenv()/setfenv() Empty
PostSubject: Re: Tutorial ~ getfenv()/setfenv()   Tutorial ~ getfenv()/setfenv() Empty

Back to top Go down
 
Tutorial ~ getfenv()/setfenv()
Back to top 
Page 1 of 1
 Similar topics
-
» Tutorial on: 'for' loops
» Instance.new() tutorial
» Metatables Tutorial
» LocalParts
» My first tutorial ~ Metatables

Permissions in this forum:You cannot reply to topics in this forum
A Roblox Community :: Tutorials and Resources :: Advanced-
Jump to: