generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: _G recreation didn't work.

Previous Thread :: Next Thread 
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
17 Jan 2015 07:38 AM
I'm trying to recreate _G, except with a catch.

Everything in _G can be indexed in any script without using _G.

If you've ever used Lua you'd know that _G is the global enviornment, and in _G is all your globals. So if you use

x = 5

in one script, and then you used

print(x)

in another, (a repeat may be needed to make sure it exists) it would print 5.


So I'm trying to create a ModuleScript, that has a Setup function, to allow a script these functionalities. This is what I have so far:

local module = {}

module.Setup = function()
local global_variables = setmetatable({
UDim2 = UDim2,
setfenv = setfenv,
LoadLibrary = LoadLibrary,
Stats = Stats,
tick = tick,
Region3 = Region3,
Spawn = Spawn,
stats = stats,
next = next,
print = print,
Delay = Delay,
Workspace = Workspace,
ElapsedTime = ElapsedTime,
workspace = workspace,
pairs = ipairs,
load = load,
elapsedTime = elapsedTime,
os = os,
ypcall = ypcall,
CFrame = CFrame,
assert = assert,
newproxy = newproxy,
ipairs = ipairs,
version = version,
game = game,
rawset = rawset,
shared = shared,
time = time,
Region3int16 = Region3int16,
_VERSION = _VERSION,
Version = Version,
table = table,
Wait = Wait,
pcall = pcall,
tostring = tostring,
settings = settings,
loadstring = loadstring,
Color3 = Color3,
CellId = CellId,
Vector2 = Vector2,
collectgarbage = collectgarbage,
UserSettings = UserSettings,
_G = _G,
delay = delay,
UDim = UDim,
Game = Game,
Ray = Ray,
error = error,
spawn = spawn,
Instance = Instance,
wait = wait,
Vector2int16 = Vector2int16,
Faces = Faces,
math = math,
setmetatable = setmetatable,
select = select,
xpcall = xpcall,
dofile = dofile,
loadfile = loadfile,
coroutine = coroutine,
getmetatable = getmetatable,
type = type,
rawget = rawget,
DebuggerManager = DebuggerManager,
Enum = Enum,
unpack = unpack,
BrickColor = BrickColor,
rawequal = rawequal,
Axes = Axes,
string = string,
Vector3 = Vector3,
require = require,
PluginManager = PluginManager,
getfenv = getfenv,
tonumber = tonumber,
printidentity = printidentity,
warn = warn,
Vector3int16 = Vector3int16,
gcinfo = gcinfo
}, _G)
for i,v in pairs(getfenv(0)) do
rawset(_G, i, v)
end
setfenv(0, setmetatable({}, {
__index = global_variables,
__newindex = function(t, k, v)
rawset(_G, k, v)
end,
__metatable = "This metatable is locked!"
}))
end

return module


I tried testing it with 2 other scripts in workspace:

require(workspace._GFix):Setup()
x = 5

--and the other one

require(workspace._GFix):Setup()
repeat wait() until x
print(x)



and absolutely nothing printed to the output. Any help here? I'm probably just overlooking something in my metatables.
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
17 Jan 2015 07:44 AM
local pfenv = getfenv(1)
setfenv(1, setmetatable({}, {__index = function(Self, Key) if pfenv[Key] then return pfenv[Key] else return _G[Key] end end, __newindex = function(Self, Key, Value) rawset(Self, Key, nil) _G[Key] = Value end}))
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
17 Jan 2015 07:49 AM
@Jarod I don't see how that would work.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
17 Jan 2015 07:58 AM
Slight fix, still don't work:

local module = {}

module.Setup = function()
local global_variables = setmetatable({
UDim2 = UDim2,
setfenv = setfenv,
LoadLibrary = LoadLibrary,
Stats = Stats,
tick = tick,
Region3 = Region3,
Spawn = Spawn,
stats = stats,
next = next,
print = print,
Delay = Delay,
Workspace = Workspace,
ElapsedTime = ElapsedTime,
workspace = workspace,
pairs = ipairs,
load = load,
elapsedTime = elapsedTime,
os = os,
ypcall = ypcall,
CFrame = CFrame,
assert = assert,
newproxy = newproxy,
ipairs = ipairs,
version = version,
game = game,
rawset = rawset,
shared = shared,
time = time,
Region3int16 = Region3int16,
_VERSION = _VERSION,
Version = Version,
table = table,
Wait = Wait,
pcall = pcall,
tostring = tostring,
settings = settings,
loadstring = loadstring,
Color3 = Color3,
CellId = CellId,
Vector2 = Vector2,
collectgarbage = collectgarbage,
UserSettings = UserSettings,
_G = _G,
delay = delay,
UDim = UDim,
Game = Game,
Ray = Ray,
error = error,
spawn = spawn,
Instance = Instance,
wait = wait,
Vector2int16 = Vector2int16,
Faces = Faces,
math = math,
setmetatable = setmetatable,
select = select,
xpcall = xpcall,
dofile = dofile,
loadfile = loadfile,
coroutine = coroutine,
getmetatable = getmetatable,
type = type,
rawget = rawget,
DebuggerManager = DebuggerManager,
Enum = Enum,
unpack = unpack,
BrickColor = BrickColor,
rawequal = rawequal,
Axes = Axes,
string = string,
Vector3 = Vector3,
require = require,
PluginManager = PluginManager,
getfenv = getfenv,
tonumber = tonumber,
printidentity = printidentity,
warn = warn,
Vector3int16 = Vector3int16,
gcinfo = gcinfo
}, {
__index = _G
})
for i,v in pairs(getfenv(0)) do
rawset(_G, i, v)
end
setfenv(0, setmetatable({}, {
__index = global_variables,
__newindex = function(t, k, v)
rawset(_G, k, v)
end,
__metatable = "This metatable is locked!"
}))
end

return module
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
17 Jan 2015 08:03 AM
Seranok told me I had to hardcode a table. Thank you Jarod for shortening it by like 20 lines or so:

local module = {}

module.Setup = function()
local __env = getfenv(0)
for i,v in pairs(__env) do
rawset(_G, i, v)
end
setfenv(0, setmetatable({}, {
__index = function(t, k)
if __env[k] then
return __env[k]
else
return _G[k]
end
end,
__newindex = function(t, k, v)
rawset(_G, k, v)
end,
__metatable = "This metatable is locked!"
}))
end

return module

But still nothing.
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
17 Jan 2015 08:03 AM
See if this fixes it.


local module = {}

module.Setup = function()
local global_variables = setmetatable({
UDim2 = UDim2,
setfenv = setfenv,
LoadLibrary = LoadLibrary,
Stats = Stats,
tick = tick,
Region3 = Region3,
Spawn = Spawn,
stats = stats,
next = next,
print = print,
Delay = Delay,
Workspace = Workspace,
ElapsedTime = ElapsedTime,
workspace = workspace,
pairs = ipairs,
load = load,
elapsedTime = elapsedTime,
os = os,
ypcall = ypcall,
CFrame = CFrame,
assert = assert,
newproxy = newproxy,
ipairs = ipairs,
version = version,
game = game,
rawset = rawset,
shared = shared,
time = time,
Region3int16 = Region3int16,
_VERSION = _VERSION,
Version = Version,
table = table,
Wait = Wait,
pcall = pcall,
tostring = tostring,
settings = settings,
loadstring = loadstring,
Color3 = Color3,
CellId = CellId,
Vector2 = Vector2,
collectgarbage = collectgarbage,
UserSettings = UserSettings,
_G = _G,
delay = delay,
UDim = UDim,
Game = Game,
Ray = Ray,
error = error,
spawn = spawn,
Instance = Instance,
wait = wait,
Vector2int16 = Vector2int16,
Faces = Faces,
math = math,
setmetatable = setmetatable,
select = select,
xpcall = xpcall,
dofile = dofile,
loadfile = loadfile,
coroutine = coroutine,
getmetatable = getmetatable,
type = type,
rawget = rawget,
DebuggerManager = DebuggerManager,
Enum = Enum,
unpack = unpack,
BrickColor = BrickColor,
rawequal = rawequal,
Axes = Axes,
string = string,
Vector3 = Vector3,
require = require,
PluginManager = PluginManager,
getfenv = getfenv,
tonumber = tonumber,
printidentity = printidentity,
warn = warn,
Vector3int16 = Vector3int16,
gcinfo = gcinfo
}, {
__index = _G
})
for i,v in pairs(getfenv(2)) do
rawset(_G, i, v)
end
setfenv(2, setmetatable({}, {
__index = global_variables,
__newindex = function(t, k, v)
rawset(_G, k, v)
end,
__metatable = "This metatable is locked!"
}))
end

return module
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
17 Jan 2015 08:13 AM
__newindex isn't even setting it to _G

What's wrong here -_-
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
17 Jan 2015 08:14 AM
local Module = {
Setup = function()
local pfenv = getfenv(2)
setfenv(2, setmetatable({}, {
__index = function(Self, Key)
--The only problem with this line is that it returns false instead of nil,
--    if the key can't be found.
return pfenv[Key] or _G[Key]
end,
__newindex = function(Self, Key, Value)
rawset(_G, Key, Value)
rawset(Self, Key, nil)
end
}))
end
}

return Module
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
17 Jan 2015 08:24 AM
I'm really against using 2, incase they call it within a function. So I tried this:

local Module = {}

Module.Setup = function()
local global = 1
while wait() do
local success,_ = pcall(function() local x = getfenv(global+1) end)
if success then
global = global + 1
else
break
end
end
local __env = getfenv(global)
setfenv(global, setmetatable({}, {
__index = function(Self, Key)
return __env[Key] or _G[Key]
end,
__newindex = function(Self, Key, Value)
rawset(_G, Key, Value)
rawset(Self, Key, nil)
end
}))
end

return Module


But the same result came. Absolutely nothing.
Report Abuse
digpoe is not online. digpoe
Joined: 02 Nov 2008
Total Posts: 9092
17 Jan 2015 08:44 AM
setfenv(1, _G)


afaik that should work
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
17 Jan 2015 08:58 AM
@above

That would nil print, and all the other global variables.
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
17 Jan 2015 09:07 AM
local xenv = getfenv(2);
setfenv(2, setmetatable(_G, {__index = function(_,k) return _G[k] or xenv[k] end; __newindex = function(_,k,v) if not xenv[k] then rawset(_G,k,v) end}))

Not like that, I suppose?
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
17 Jan 2015 09:08 AM
I already said something nearly identical to that, eLunate. Not quite like it, but yeah.

Copier. loljk
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
17 Jan 2015 09:08 AM
@eLunate It has to work from a ModuleScript.
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
17 Jan 2015 09:14 AM
Yeah sorry Jarod, I went up and read your after I'd posted mine and it -is- very much the same in practice.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image