of     2   
chevron_rightchevron_rightchevron_right

Waffloid
#183610366Sunday, February 14, 2016 8:18 PM GMT

setfenv(1,setmetatable({},{})) Am I the only one who finds that 1 line of code (which works and doesn't error) absolutely amazing? You can track new variables or make variables do weird things when they're referenced (e.g. kek = 5 print(kek), prints 23)
cntkillme
#183610808Sunday, February 14, 2016 8:25 PM GMT

Once you have a basic understanding of metatables and environment, you'll realize it's really not that special. Especially since local variables are not stored in the environment and most people use locals.
cntkillme
#183610952Sunday, February 14, 2016 8:27 PM GMT

environments*
cody123454321
#183611548Sunday, February 14, 2016 8:41 PM GMT

If you like that, check out my models Class Module and Function Environment GO BOOM!!!! cuz they are pretty neat.
cpmoderator12345
#183612116Sunday, February 14, 2016 8:49 PM GMT

well.... to me it looks like it appends a blank metatable to a blank table, and sets the table as the environment... oh wait actually that could be good we can troll people and make operators do the opposite thing https://www.youtube.com/watch?v=onhFH7jpq2c
Agluk15
#183612375Sunday, February 14, 2016 8:53 PM GMT

@CpModerator Please tell.
cpmoderator12345
#183613785Sunday, February 14, 2016 9:10 PM GMT

nvm you cant i mean you could but you would need the debug.setmetatable, not the normal setmetatable https://www.youtube.com/watch?v=onhFH7jpq2c
Agluk15
#183613878Sunday, February 14, 2016 9:12 PM GMT

Oh, because I was skeptical on reversing + and - and whatnot.
cpmoderator12345
#183614061Sunday, February 14, 2016 9:14 PM GMT

i forgot that setmetatable was restricted to tables only but debug.setmetatable isnt with debug.setmetatable, you can give numbers a metatable, which affects all numbers so then thats how you give numbers a metatable https://www.youtube.com/watch?v=onhFH7jpq2c
Agluk15
#183614248Sunday, February 14, 2016 9:17 PM GMT

This would be cool: debug.setmetatable(+,-) debug.setmetatable(-,+) However that doesn't work because you can't pass a function a syntactical argument, and after the first line + is - so - would still be -.
cpmoderator12345
#183614455Sunday, February 14, 2016 9:19 PM GMT

no local x = 5 debug.setmetatable(x,{_add=function(a,b) return a-b end}) print(x+3) -- output should be 2 if using vanilla lua https://www.youtube.com/watch?v=onhFH7jpq2c
cntkillme
#183614469Sunday, February 14, 2016 9:19 PM GMT

-.- why do people talk about stuff they don't understand
cpmoderator12345
#183614542Sunday, February 14, 2016 9:20 PM GMT

idk why it shows 8 though, but i read about debug.setmetatable a long time ago so maybe its for a previous version https://www.youtube.com/watch?v=onhFH7jpq2c
cntkillme
#183614906Sunday, February 14, 2016 9:25 PM GMT

No, it's not for a previous version lol you just don't know how it works
Waffloid
#183615096Sunday, February 14, 2016 9:28 PM GMT

the main reason i found this interesting is because setmetatable(getfenv(),{}) errors, so i thought it was impossible till' i did that
cntkillme
#183615516Sunday, February 14, 2016 9:34 PM GMT

It only errors because setfenv expects a function (or afunction based on the call stack), getfenv() returns a table.
TickerOfTime
#183617382Sunday, February 14, 2016 10:02 PM GMT

And the metatable of the environment (IIRC) is locked. Also, if you swapped "+" and "-", wouldn't they end up doing the same thing at the end? Either that or a stack overflow. Because indexing directly in "__index" results in a stack overflow, so ye, have fun.
cntkillme
#183618187Sunday, February 14, 2016 10:12 PM GMT

only on roblox is the global env of scripts locked. and you can't "swap" + and -, and I don't see how on earth that would lead to a stack overflow. indexing in __index doesn't cause a stack overflow, it's only when you index that same table that has that __index metamethod in the first place.
TickerOfTime
#183621738Sunday, February 14, 2016 11:05 PM GMT

yes cnt. To "swap" "+" and "-", you might so something like this: ``` debug.setmetatable(0, { __add = function(A,B) return A - B end, __sub = function(A,B) return A + B end }) print(5 + 5) ``` ~That _would_ cause a stack overflow~. Nevermind, it doesn't. huh. ~ Shakespeare's Soggy Left Nut
cpmoderator12345
#183622986Sunday, February 14, 2016 11:21 PM GMT

that code doesnt even work for me https://www.youtube.com/watch?v=onhFH7jpq2c
cntkillme
#183623324Sunday, February 14, 2016 11:25 PM GMT

No it wouldn't, learn metatables. Metamethods are FALLBACKS. FALLBACKS: `an alternative plan that may be used in an emergency. "teaching was a last resort, a fallback"`
TickerOfTime
#183623397Sunday, February 14, 2016 11:26 PM GMT

I will swear that has worked before.. ~ Shakespeare's Soggy Left Nut
cpmoderator12345
#183623773Sunday, February 14, 2016 11:32 PM GMT

i literally made a file that did the exact same thing as that and it didnt work wait i used lua 5.1.4 so it should work though https://www.youtube.com/watch?v=onhFH7jpq2c
JarodOfOrbiter
#183623940Sunday, February 14, 2016 11:34 PM GMT

When anyone asks about metatables in the future, this will be my go-to article explaining them, just because of all the wrong information.
TickerOfTime
#183631167Monday, February 15, 2016 1:14 AM GMT

Cnt you're right, they're fallbacks. I swear I remember somewhere reading or testing to the effect that you could do operator overloading with them..

    of     2   
chevron_rightchevron_rightchevron_right