Is there a method to do a IntelliSense with Cached Modules?

I’m honestly just trying to require a lot of modules without it being that messy long chunk at the top of the file/module script, so I was just curious whether or not you could type check specific modules.

Make it so when you put in a certain method like :HurtCharacter, you’ll immediately get re-directed to the module with that actual method and not get an error that it doesn’t exist in said module.

for _, v in Modules.Combat:GetChildren() do
	if v:IsA("ModuleScript") then
		moduleCached[v.Name] = require(v)
	end
end

As an example of a basic Cached Module example.

You can make use of Roblox’s built-in type annotations and tools like Roblox LSP (Language Server Protocol) which can help provide IntelliSense support.

Define the types for your module:

-- CombatModule.lua
local CombatModule = {}
type CombatModule = {
    HurtCharacter: (character: Instance) -> ()
}

function CombatModule:HurtCharacter(character)
    -- Implementation
end

return CombatModule

Create a module to cache your modules:

-- ModuleCache.lua
local Modules = script.Parent.Modules
local moduleCached = {}

for _, v in Modules.Combat:GetChildren() do
    if v:IsA("ModuleScript") then
        moduleCached[v.Name] = require(v)
    end
end

return moduleCached

Use type annotations when requiring the cached modules:

-- MainScript.lua
local moduleCached = require(script.Parent.ModuleCache)
local CombatModule: CombatModule = moduleCached["CombatModule"]

CombatModule:HurtCharacter(someCharacter)
2 Likes

This wouldn’t cause any issues if there’s something already in the table directly, such as Assets or references to stuff of some sort right?

E.G

local Animation_C = {
	Base_CombatAnimations = {

		[1] = CombatFolder.Hit1,
		[2] = CombatFolder.Hit2,
		[3] = CombatFolder.Hit3,
		[4] = CombatFolder.Hit4,
	};
	[[Block = {CombatFolder.Block};
	Dodge = {CombatFolder.Dodge};]]
}