How to create a whole seperate API including custom paths with ModuleScript

Is there a way to make variables and paths from completely out of nowhere using a ModuleScript by simply requiring one?

Here’s an example:

return { -- ModuleScript
	duna = 1,
}

A script will require it and print the written variable:

local t = require(script.ModuleScript) -- Script

print(duna) -- 1

I saw something like this in a script called EzConvert (which since I actually worked on MainModules I found that script alot in scripts) that rewrote entirely new paths in normal Scripts

If there is a way or alternative, please post here
Any help is appreciated!

I don’t think you can do that, The only thing that works with this is creating & exporting custom types via ModuleScripts (Technically it isn’t the same thing, You need to call the Module variable and then the Type):

-- Module
export type SomeType = {X: number; Y: number}
export type OtherType = string | number | boolean

return {} -- Could also be `nil`

-- Script
local TypeDefinitions = require(PathToModule)
local SomeTable: TypeDefinitions.SomeType = {
   X = 1;
   Y = 2;
}

Yeah, this is possible, but it would require using legacy globals from lua.
You’d have to inject the globals into the environment calling the specific module…

-- Module Script
local Module = setmetatable({}, { __index = getfenv() });

Module.duna = 1

return function()
	setfenv(2, Module)
end
-- Script
require(script.ModuleScript)()

print(duna)

Unknown global duna
image

Test it don’t listen to those suggestions. They don’t know what they’re talking about.

Yeah, Hence the warning legacy globals which I mentioned.
the Studio Linter will not know about the global. But it’s there, you’d have to run it.

How would I go about running it?

Click the play button to run a game, or press F8, or F5.

1 Like