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 » Game Design
Home Search
 

Re: ModuleScripts and getfenv() :(

Previous Thread :: Next Thread 
Deplexity is not online. Deplexity
Joined: 18 Nov 2011
Total Posts: 1965
10 Dec 2014 10:05 PM
So right now I have this code:

local newTest = {}

function getGameSystems(Instance)
for index,Module in pairs(Instance:GetChildren()) do
if Module and Module:IsA("ModuleScript") then
getfenv()[Module.Name] = require(Module)
end
end
end

getGameSystems(Workspace.gameSystems)

print(newTest.testfunction())

For some reason, this errors, saying:
20:04:20.488 - Workspace.testScript:16: attempt to call field 'testfunction' (a nil value)

But in the print function at the bottom, if I replace it with:

print(getfenv().newTest.testfunction())

It works fine. Is there anyway to make it so I don't have to enter getfenv() at the beginning of every variable?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
10 Dec 2014 10:46 PM
newText.getGameSystems = function(Instance)
...
end;
Report Abuse
Deplexity is not online. Deplexity
Joined: 18 Nov 2011
Total Posts: 1965
10 Dec 2014 11:00 PM
That sort of works.

Essentially what the function does is get all the ModuleScripts in a folder and sets the tables in the ModuleScript to their respective table in the code.

Just an easier way instead of having to type the require thingy for every variable, because there's going to be a lot.

One way I thought of doing this is just checking the ModuleScript's name and setting the table with the same name to the ModuleScript, but I don't know how to actually get variables names.

How would I do this?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
10 Dec 2014 11:10 PM
Just do like:

local global = getfenv(0);

local loadModuleInEnv = function(mod)
for key, value in next, require(mod) do
global[key] = value;
end
end;

So you would just do:
loadModuleInEnv(workspace.Blah);
loadModuleInEnv(workspace.Blah2);

And all the things it returns (table or function or whatever) is added to the environment. But if you want to make a table with the name of the module and with everything in it, just do:

global[mod.Name] = require(mod);
Report Abuse
Deplexity is not online. Deplexity
Joined: 18 Nov 2011
Total Posts: 1965
10 Dec 2014 11:27 PM
Thanks so much :)

Here was my finished code:

local math = {};
local table = {};
local string = {};

local loadModuleInEnv = function()
repeat wait() until Workspace:findFirstChild("gameSystems")
for index, module in next, Workspace.gameSystems:GetChildren() do
for key, value in next, require(module) do
string[key] = value;
end
end
end;

loadModuleInEnv();

string.testfunction()

> "It works!"
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
10 Dec 2014 11:27 PM
Why do you have local math = {}, blah at the top?
Replacing the math, table, and string libraries :D
Report Abuse
Deplexity is not online. Deplexity
Joined: 18 Nov 2011
Total Posts: 1965
10 Dec 2014 11:28 PM
Yes sir!
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Game Design
   
 
   
  • 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