Module Retention

Greetings, folks!

So I made this plugin to aid me in constructing nodes for a system I’m working on, and it basically clones its own module-script and then pastes it on my ServerScriptService so that I can utilize it - if I use my output bar or something, I can actually yield variables attached to this module, but for some reason it just returns nil when I hit Play.

Any reasons why this happens? Anyone have any suggestions on how to get the script to give me this module?

1 Like

We need something to work with before we can give you any answer. At least some information about what the module should return and a portion of the code.

So basically, my plugin’s code stores entities of this form

local Database = {
    ["1"] = {
        Position = Vector3.new(...),
        ID = ...,
        ...
    },
    ...
}

When I compile the module, it writes Database to another module script that I actually have in ServerScriptService

The issue is that when I compile, I can actually get stuff about Database and its relevant variables using the output bar but it yields nil and what-not when I actually try testing

Can we please get the other script context? We need more information so we can help you better :slight_smile:

You said that you can get the data with the output bar, but not the way you do it. As far as I understand, your plugin writes over a ModuleScript’s source, which might be causing issues with the way modules cache their output.

The one I write this database to? It’s actually empty, I just keep it as a dummy container for the the contents of the database

I just get data by doing stuff like

Entity = Container.Database[tostring(id)]

When I do something like that and refer to a member of that specific entity (e.g. Position, ID etc.) in the Output Bar, I actually get what I expected

When I test though, I just get nil

Are you using the server view when you attempt to require the module? When you start a session, your command bar is client-sided, so you must change to the server.

Another point to make is that the command bar runs on a separate VM to regular scripts, so it’s treated as an individual machine. If your contents are input from a plugin or regular script, the command bar will not be able to see them. The same thing happens with the Lua Chat System: try fetching a list of speakers, it’ll be empty.

If you want to be able to test your code, you will need to set up a unit test using a game script and not the command bar. You can do this by writing code in ServerStorage in a script and then moving it to ServerScriptService where it will execute.

1 Like

When I’m testing via Play, I access the module written to by requiring it on a server script - both modules are held in ServerScriptService

I used the Output Bar mainly to try figuring out why actually testing the module caused it to yield nil

It’s also weird because when my own plugin tries fetching the database back from the module I write it to, it actually gives me the data back

Going to need far more information and code samples to help you here if what I said doesn’t answer your question. You have me confused and lost and there isn’t much explanation to your issue either that I can understand.

Alright

Here’s the portion regarding the plugin

local Nodes = require(script.Nodes)

local function Compile_Script()
    local Paths = require(game.ServerScriptService.Manager.Path_Module)
    Paths.Nodes = Nodes.Nodes
    Paths.Max = Nodes.Max
end

Nodes.Nodes is the database structure I talked about

Would this suffice?

That would. So let’s redefine the problem… you’re supposedly getting nil back during test, but what are the circumstances? What is returning nil? How is it called? Where is it called? What view is active at the time? Try explaining the problem in more depth.

→ I just test by hitting the Play button in Studio

→ Whenever I refer to some member or aspect of Path_Module that I write to it by my plugin, all I get is nil - this includes Nodes and Max from my code snippet

→ I call for the Nodes and Max by just using require(...Path_Module)

→ I call for Nodes and Max in a server script held in ServerScriptService - this is also where Path_Module is actually held

→ I’ve tried it out on both Client view and Server view
I have a print in the code that calls it so I can see if the stuff is nil or not - in Client view, the server script that actually asks Path_Module for what I wrote to it just recieves and prints nil
Same thing in Server view - it just prints nil irrespective of how I try getting Path_Module’s contents

Scripts, LocalScripts and the command bar can’t see what a plugin writes to a script if I recall correctly. The plugin gets it’s own copy of a module on require and other scripts can’t see the changes made. Only cause I can think of.

Where and when is Compile_Script ran? If your plugin is meant to do this work pre-compile time (in Studio), you will need to write to ModuleScript.Source by converting your modules into strings and going from there, rather than requiring both modules and placing data through standard convention.

Ah, fascinating

The plugin is meant to do all that before I actually run the game - the compiled module is meant to be referred to later on by the other scripts in the system

I’ll definitely check out this source editing via strings - thanks!

Just a warning that working with sources can be tedious and annoying. If it’s at all possible, try to avoid working with two modules and work with only one. Your plugin can hold temporary data in some form and when it compiles to a script, it writes out the data in Source accordingly.

I’m not familiar with this kind of writing and I don’t have a use for it either, but I’m at least aware of the mess it could rouse.