Import all functions, like Python. (import *)

Hey! I wanted to create a function that would automatically implement functions globally. Such that I could call use(table) and all values from the table would be global.

Example:

use({
    hello = function() print('Hello World') end
})

hello()

You would be able to call hello() without indexing the table.
Unfortunately, that doesn’t work in roblox.

Here’s some edited code from Stack Overflow, that works in normal lua, but not in Luau:

function use(module, ...)
	for k,v in pairs(module) do
		if not _G[k] then
			_G[k] = module[k]
		end
	end
end

local classes = {}
function classes.Hello(tab)
	print(tab[1])
end

use(classes)

Hello({'hello', 'world'})

The reason it doesn’t work is because in Luau, you have to index _G to use these global functions.

Hence running _G.Hello() works.

Is there a bypass for this? I want to use the functions without needing to specify _G or index an existing table variable.

Also yes I know that I wrote some of the example code pretty quickly and didn’t really think of formatting it much.

2 Likes

If you’re wondering what the uses for this could be by the way, it would be useful for making custom import functions. Such as a custom import { Hello } from table.

Or to only have to run require() and all the functions are automatically loaded for you.

I want to make a feature request but I have a feeling it’ll be rejected. (None of my other requests really have gotten much attention either :sweat_smile:)


The only other way I think this could work would be returning _G each time, but that means I can’t return a metatable or use the function like it’s supposed to be used:
image

if you want all functions to be accessible without indexing them, you’ll have to manually assign each one with local

local module = require(game.ReplicatedStorage.Module)
local doThis = module.doThis
local doThat = module.doThat

doThis()
doThat()

or if you really don’t want to assign them you can probably use setfenv and a metatable with __index, but I really don’t suggest doing that

2 Likes

Yes, I know. setfenv() and getfenv() seem to work. Although it does give a deprecation warning. It also stops autocomplete and gives an “unknown global” warning for the thing I added using the Env.

this is why i don’t suggest using setfenv. Setfenv replaces the entire environment of a script or a function, this includes the setfenv global and print global. You can try doing this

local env = getfenv()
env['doThis'] = module.doThis
setfenv(1, env) 

if that doesn’t work you’ll have to manually assign each globals name to itself again, I have that here though

return {
    ["_G"] = _G,
	["assert"] = assert,
	["Axes"] = Axes,
	["BrickColor"] = BrickColor,
	["bit32"] = bit32,
	["buffer"] = buffer,
	["CFrame"] = CFrame,
	["Color3"] = Color3,
	["coroutine"] = coroutine,
	["CatalogSearchParams"] = CatalogSearchParams,
	["ColorSequenceKeypoint"] = ColorSequenceKeypoint,
	["ColorSequence"] = ColorSequence,
	["collectgarbage"] = collectgarbage,
	["delay"] = delay,
	["debug"] = debug,
	["DateTime"] = DateTime,
	["DockWidgetPluginGuiInfo"] = DockWidgetPluginGuiInfo,
	["Delay"] = Delay,
	["error"] = error,
	["Enum"] = Enum,
	["ElapsedTime"] = ElapsedTime,
	["elapsedTime"] = elapsedTime,
	["Font"] = Font,
	["File"] = File,
	["Faces"] = Faces,
	["FloatCurveKey"] = FloatCurveKey,
	["game"] = game,
	["getfenv"] = getfenv,
	["getmetatable"] = getmetatable,
	["gcinfo"] = gcinfo,
	["Game"] = Game,
	["Instance"] = Instance,
	["ipairs"] = ipairs,
	["loadstring"] = loadstring,
	["math"] = math,
	["newproxy"] = newproxy,
	["NumberRange"] = NumberRange,
	["NumberSequenceKeypoint"] = NumberSequenceKeypoint,
	["next"] = next,
	["NumberSequence"] = NumberSequence,
	["OverlapParams"] = OverlapParams,
	["os"] = os,
	["plugin"] = plugin,
	["print"] = print,
	["pcall"] = pcall,
	["pairs"] = pairs,
	["PhysicalProperties"] = PhysicalProperties,
	["PathWaypoint"] = PathWaypoint,
	["printidentity"] = printidentity,
	["require"] = require,
	["Random"] = Random,
	["rawget"] = rawget,
	["RaycastParams"] = RaycastParams,
	["RotationCurveKey"] = RotationCurveKey,
	["rawset"] = rawset,
	["rawequal"] = rawequal,
	["Rect"] = Rect,
	["rawlen"] = rawlen,
	["Ray"] = Ray,
	["Region3"] = Region3,
	["Region3int16"] = Region3int16,
	["select"] = select,
	["shared"] = shared,
	["string"] = string,
	["spawn"] = spawn,
	["setmetatable"] = setmetatable,
	["setfenv"] = setfenv,
	["Secret"] = Secret,
	["settings"] = settings,
	["SharedTable"] = SharedTable,
	["stats"] = stats,
	["Stats"] = Stats,
	["Spawn"] = Spawn,
	["table"] = table,
	["TweenInfo"] = TweenInfo,
	["tostring"] = tostring,
	["type"] = type,
	["tonumber"] = tonumber,
	["tick"] = tick,
	["task"] = task,
	["typeof"] = typeof,
	["time"] = time,
	["UDim2"] = UDim2,
	["unpack"] = unpack,
	["utf8"] = utf8,
	["UDim"] = UDim,
	["UserSettings"] = UserSettings,
	["Vector3"] = Vector3,
	["Vector"] = Vector2,
	["Vector3int16"] = Vector3int16,
	["Vector2int16"] = Vector2int16,
	["Version"] = Version,
	["version"] = version,
	["workspace"] = workspace,
	["wait"] = wait,
	["warn"] = warn,
	["Wait"] = Wait,
	["Workspace"] = Workspace,
	["xpcall"] = xpcall,
	["ypcall"] = ypcall,
}

That seems like way more than you’d need to. I doubt it will even show up in autocomplete (it’s deprecated as such) and will still pump warnings at you no matter the case.

Luau also states that they’ll never un-sandbox the _G global and you’ll need to index it using _G. I don’t find that too bad, but it’s annoying.

Also the Roblox Script Editor needs some major revamping :sweat_smile:

Use require() on module scripts.
You can create a module script with functions in it.
require() it and store it to a variable.
You would then be able to call the functions and values of that module from your script.

yeah it will definitely not show up in autocomplete since not even normal metatables get autocomplete. But other than that there isn’t anyway to import everything to the environment

Yeah that’s the thing that I already do. It’s what I believe most people do when using module’s in Roblox, and also with Lua in general.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.