Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Module Scripts and Variables?

I was wondering if there was a way/if it is possible to transfer a variable from one Module Script to another Module Script.

I've tried to look up a way to do it, but there aren't any good articles/guides about it.

If you can assist me, please show me how to do it or send me a link to an 'article' that's useful.

Thank you!

1 comment

0

Edited my answer to add the new code.

1 answer

Return it

The thing about ModuleScripts is that they can return values. That means for you that you can simply use the return of the ModuleScript to share values - If you return a table, then you can treat it just like any other table and use it to share variables.


You can however make a function which will make a script share the same global variables as another.

local env = getfenv(0); -- Get the default environment
local niproxy = function(_,k,v)
	-- If a table tries to set something, ignore it.
	env[k] = v; -- And instead send it to the global proxy
end;
return function()
	local _env = getfenv(2); -- Get the script environment
	local script = _env.script; -- Preserve the script object
	for k,v in next, _env do -- Iterate through the environment
		if k ~= 'script' then -- Ignore the script object
			env[k] = v; -- Copy variables into the global table
		end;
	end;
	setfenv(2, setmetatable({script = script},{__index = env, __newindex = niproxy}));
end;

If you put that in a ModuleScript and then require it and call the function, all global variables will be shared with any other scripts that do the same thing.

1 comment

0

Thank you for answering and yes I would like/need something like that!

This discussion is preserved as it was. New answers, comments, and votes are closed.