Can Modules store instances?

As the title says, I want to know if a “MainModule” can store instances and use them later for other game places to use

2 Likes

Based on my knowledge, it can store Instances but it will nil in other game/places, Instances cannot replicated between places.

1 Like

However if the instance is under the ModuleScript or the referenced Instance is parented to anything that is parented to the ModuleScript, the reference will still the same between games assuming you transfer the ModuleScript with the instances along

From my experience, you can most certainly return an instance from a module.

An example of this would be:

-- module script
local make_part = function(color,transparency,position,size)
    local part = Instance.new("Part")
    part.Color = color
    part.Transparency = transparency
    part.Position = position
    part.Size = size
    return part
end

-- normal script
local create_part = require(workspace.make_part)
local part = create_part(Color3.new(0.5,0.5,0.5), 0.5, Vector3.new(2,4,8), Vector3.new(2,2,2))
part.Parent = workspace

There is a lot (and I mean a lot) of other stuff you can do with this but this is just an example.

You’ll need to use DataStores if you want objects to persist across multiple server instances of the same place.

You should understand that instances doesn’t belong to your code. your code doesn’t have any ownership over it. your variables or modules only holds references to objects, but the engine is the one who have the ownership over all instances, and you’re only allowed to reference them and signal the engine to do something with them, like change their color or destroy them.

Conclusion:

  • Your script doesn’t store the instance itself but rather just references to them
  • So your stored reference after the studio restarted (even if that’s possible) doesn’t mean anything, as the actual object is gone for good when the game have shut down.
  • Your code can’t store the object itself but it’s opaque properties (number, string, bool…) so you can store that information, and use it to reconstruct the object whenever needed!

Code example:

-- Tell the engine to create a part and return a reference to the part so we can infer action or signals to it...
local part = Instance.new("Part")
-- Tell the engine to change the color of the object with the reference "part"
part.Color3 = Color3.new(0,0,0)
-- Create a new reference that refers to the same exact object
local part2 = part
-- Tell the engine to change the transparency property with the reference "part2"
part2.Transparency = 0.26
-- Request the property "Transparency" of the part referenced by "part"
print(part.Transparency) --> 0.26
-- Tell the engine to destroy the object referred by "part"
part:Destroy()
-- Now part2 is also destroyed because it's refering to the exact same thing but the 2
--variables are still accesible since the object got destroyed but the reference is still valid.

Funny example:
Let’s say I don’t have 10000$ but I told my friend that I do. well yea I can refer to that, but when it comes to actually show up that money and use it, you lost the game : )
You can’t have money just because you refer to it and talk about it. I wish it was that easy : /

2 Likes

If it’s parented to mainmodule it should work