How To Edit ModuleScript With A Plugin

I’m in the middle of developing a game. What I need to do is in a ModuleScript, I need to script 39 tables with some variations in the values of the table. What I am trying to do is create a plugin to do all that repetitive work for me. The problem I am having is I don’t really know how I would edit the ModuleScript through script.Source.

Here is an example of the module:

This text will be hidden

local module = {}
module.Default = {

["Cards"] = {
--This is the table I need to duplicate 39 more times with the key coutning up to 40
--[[Key -->]]["one"] = {
["Color"] = 0,
["Value"] = 0,
["Title"] = 0
          }
     }
--End of table
}
return module

The complete product would look something like this:

local module = {}
module.Default = {
["Cards"] = {
     ["one"] = {
          ["Color"] = 0,
          ["Value"] = 0,
          ["Title"] = 0
               }
       },
     ["two"] = {
          ["Color"] = 0,
          ["Value"] = 0,
          ["Title"] = 0
             }
        },
     ["three"] = {
          ["Color"] = 0,
          ["Value"] = 0,
          ["Title"] = 0
               }
        }
}
return module

EDIT:
I know that you can set the soruce of the code however, I’m looking to edit preexisting code and to add on to the preexisting code.

module.Source = string.format(module.Source, [[
    Some stuff here
]])

Instead of editing the script’s source you can just generate a table and then print it in the output (Only works with the expressive output that is beta I believe) then copy it and manually paste it into the script. That’s what I usually do and it works great at least for me.

1 Like

Although this would work, the whole point of editing the script through a plugin is so that I don’t have to copy and paste 39 times and edit all the values.

Understandable. What I do is I make all 39 tables inside of a table itself similarly to this and then I copy that entire table of 39 sub-tables into the main table. Then you can just remove the table so all sub-tables are directly inside the main table.
In terms of showing this what I made would do this

{
    ["one"] = {
      "5";  
    };
    ["two"] = {
        "15"
    };
}

Then I paste that entire part into my main table and remove the outer table so I have 2 tables directly in the main. With this you generate the entire table through code avoiding copy pasting 39 tables

If I can’t figure out how to do this through a plugin then I will use this method. But I still prefer to do this through a plugin, figuring this out would also help me create a plugin to quickly edit the values of a card without going through the ModuleScript.

Yeah definitely. In terms of editing the source as I see this as your main goal I suppose you can do something along the lines of

module.Source = string.format(module.Source, [[
    Some stuff here
]])

Then you would use %s in the table to actually add the values appropriately. This is NOT tested and I have little to no experience with editing the source of a script but I suppose it would work. Also be careful as it can possibly delete some stuff so be sure to have a backup

Alright thanks, I’ll try to mess around with this.

I just had and idea, so usually when you update a script through a script, it’s only for the session, so I think in theory what I could so insert the tables as you would with a regular script and then update the source code of the script to the updates source code that’s in the session.

When you say session do you mean like during runtime or just when in studio? Afaik you cannot edit the script source in an actual script only in command bar and plugins.

Afaik you cannot edit the script source in an actual script only in command bar and plugins.

Pretty sure I mean runtime. And I know that I can only edit the script through the plugin. I have a function in the module that allows me to update the module. So in theory I should be able to update the module and hopefully, the source code from that get’s updated as well in the backend and so I can update the source with the plugin.

If I understand correctly you wanna update the module using the plugin during runtime correct? I am not too sure if that is possible or not but I suppose you can give it a try. Let me know if it works or not

Yup that’s right. I’ll let you know.

I am not too sure if that is possible or not but I suppose you can give it a try. Let me know if it works or not

Welp that failed.

Sorry about the slightly late reply was in school. Anyways I actually made something that does something along the lines of what you want though you’d have to play around with it of course.

-- edit source
local module = game.ServerScriptService.ModuleScript

local data = { -- The things we want to add into the other table
    ["two"] = {
        Value = math.random(1, 1e3)
    };
    ["three"] = {
        Value = math.random(1, 1e3)
    }
}


local fullString = ""

for index, value in pairs(data) do
    local combinedString = ("[%q] = {\n"):format(index)
    for index, value in pairs(value) do
        combinedString = combinedString .. ("\t\t%s = %s;"):format(tostring(index), tostring(value))
    end
    combinedString = combinedString .. ("\n\t%s\n\t"):format("};")
    fullString = fullString .. combinedString
end

module.Source = string.format(module.Source, fullString)
1 Like

I don’t think you will understand how thankful I am. I literally spent 6 hours trying to find a soluation. Thank you, thank you, thank you!

Hopefully you find it useful. One key thing to note is it does not support nested tables inside the other data tables. If you need that let me know and I can probably add that to it. Should also mention you’d have to run it in command bar or in a plugin environment

Oh, I need it for nested tables. This module is kind of huge and has tables within tables so it would be great if it worked with nested tables.

So I finally think I got it after a lot of pain and suffering.

-- edit source
local module = game.ServerScriptService.ModuleScript

local data = { -- The things we want to add into the other table
	["two"] = {
		Value = math.random(1, 1e3);
		index = {
			x = 5
		}
	};
	["three"] = {
		Value = math.random(1, 1e3);
		index = {
			value = {
				part = 5
			}
		}
	};
	["four"] = 5;
}

local function tableToString(_table, depth)
	local completedString = ""
	for index, value in pairs(_table) do
		local tabs = string.rep("\t", depth)
		local combinedString = "%s = %s;\n"
		if type(value) == "table" then
			combinedString = tabs .. ("[%q] = {\n%s"):format(index, tableToString(value, depth+1)) .. ("\n%s%s\n"):format(tabs, "};")
		else
			combinedString = tabs .. combinedString:format(index, value)
		end
		completedString = completedString .. combinedString
	end
	return completedString
end

local _string = tableToString(data, 1) -- If your table already has been indented change this to how many times it has been indented
module.Source = string.format(module.Source, _string)

The way you’d use this is as simple as just using this code in a module script

local module = {
	["one"] = {
		Value = 1;
	};
%s	
}
return module
2 Likes

This will work in this case, thank you! I wonder if there’s a way to actually edit preexisting values, instead of inserting them just editing a preexisting value.

This will work in this case, thank you! I wonder if there’s a way to actually edit preexisting values, instead of inserting them just editing a preexisting value.

Actually, I just had an idea. In a plugin, what I could is set up a gui to edit the values of the table then have it instead of replacing %s to replace whatever table I am editing.