Its a good ideia load all instances in a table?

Hello.

I was thinking about nothing and BOOM. I have a brilliant ideia to create a module whose function is load all instances and return as a table.

local DataLoader = require(<somewhere>)

local Instances = DataLoader.Load(workspace)
Instances.Loaded:Wait()

Instances["SomePart"].Size = Vector3.one*5

Is this a good ideia? :thinking:

1 Like

It is probably not a good idea, but really depends on what you would need it for?

2 Likes

Why isnt a good idea? I can use this for a lot of things actually.

local DataLoader = require>..
-- A boolean paramater to define if wait or no
local Instances= DataLoader.Load(worksapce, true) -- This will wait all instances load

I can create to separate classes, like:

local T = DataLoader.Load(workspace, true)
-- FOLDER          INSTANCES
T["Structures"]["Chair"].Parent = nil
1 Like

Depends how many instances you have.

If you typed them all out in your script, if there’s so many you might end up hitting the local variable limit, then yes.

If you typed them all out, and it made your code look messy, then yes.

But, a long table indexer is annoying to read through too, and may not be the best on memory compared to one table index (yes, ik its only a microoptimisation)

So, what I would suggest is grouping related instances and any functions to manipulate them into one table, as well as other related data, and have many tables of that. A bit like namespaces in other languages. It’ll keep them all organised and look much nicer while not being as harsh on memory (again, microoptimisation)

You can define them all in your module and require it, but modules have to return exactly one value.

So, you can do something like this:

--module

local instances1 = {
    ["SomeInstance"] = workspace:FindFirstChild("SomeInstance"),
    ["RandomFunction"] = function() end,
    ["OtherData"] = 1
}

local instances2 = {
    ["SomeInstance2"] = workspace:FindFirstChild("SomeOtherInstance"),
    ["ADifferentFunction"] = function() end,
    ["OtherData"] = "hi"
}

return {instances1, instances2}
--script
local instances1, instances2 = table.unpack(require(--[[path to module]]))

--etc.
2 Likes