ToTable & ToInstance - Convert tables to instances and vice versa

Introduction

Hello!

I’ve created this module to allow individuals to easily convert their tables to instances, and convert those instances back to tables. I created it for my own personal use but decided I may be helping individuals who need it if I provided it for everyone to use.

In terms of customizability, unfortunately there is none that I made readily available but since it’s open-sourced, you can feel free to edit it to your own liking.

Questions you may have

Does it support nested folders and tables?

It does! This module relies heavily on recursion which is one thing I had a hard time with when I first started. I’m hoping this module aids users with datastores and recursion.

What datatypes does it support?

The basic version supports boolean, table, number and string. The advanced version supports boolean, table, number, string, BrickColor, Vector3, CFrame, Color3 and Ray.

Functions:

Module:ToInstance(table, parent)
Converts table variable to instances and parents it to the parent variable assigned above.

Module:ToTable(parent)
Parent of the instances which you want to compile into a table.

You can get the module here:

Second, more advanced option that includes more datatypes:

Advanced Functions:

Module:ToInstance(table, parent)
Converts the table variable to instances which are parented to the parent argument.

Module:ToTableForDataStore(folder)
Converts the instances from the children of the provided folder argument.

Example of how I use it personally:

local data = {
    ['Coins'] = 0;
    ['Inventory'] = {
        ['Accessories'] = {};
        ['Gear'] = {};
        ['Trails'] = {};
        ['Packages'] = {};
    };
    ['Passes'] = {
        ['Premium'] = false;
    };
    ['Rank'] = 'AlphaTester';
    ['Permission'] = 1;
    ['BanInfo'] = {
        ['Banned'] = false;
        ['BanReason'] = 'N/A';
        ['UnbanTime'] = 'N/A';
    };
    ['Settings'] = {
        ['UIPrimaryColour'] = {
            ['R'] = 86;
            ['G'] = 146;
            ['B'] = 127;
        };
        ['UISecondaryColour'] = {
            ['R'] = 255;
            ['G'] = 255;
            ['B'] = 255;
        };
        ['UIFont'] = 'SourceSans';
        ['GlobalShadows'] = true;
        ['PlayerMode'] = 'Visible';
        ['ResetKeybind'] = 'R';
    };
}

Converts to:

This is available on GitHub as well:

35 Likes

Oh yes, I like this, though just like my own module I find it kind of “useless”. Its very interesting but never came useful to me… it’s cool to save leaderstats into tables and back again, though you made a better implementation of it, mine was my first module so it wasn’t great lol, instead of doing parent you had to do module.storage :eye::lips::eye: it was a pain, i might come to update that later

None the less for what it is it’s pretty great, it seems pretty good for it does and very well optimized too; (i assume)

2 Likes

Just a thing, I’m not sure if you’re doing this but you should check if you’re converting the tables indexes into numbers if they are numbers, on my module I remembered you can’t really do ipairs since it’s just a string with a number on it-

(Since you convert them into instances their names should be their indexes, aka numbers will become string on object.Name)

Btw my likes ran out, i prob read your post, don’t worry, but i can’t like :confused:

1 Like

I mainly made it for organizational purposes. I like to create subfolders for values that are supposed to be grouped (like BanInfo)

As for your other reply, I’m not using ipairs. Value names become the index and their value becomes the value. I’ve been using it for loads of testing up to this point and haven’t come across any issues which haven’t been fixed before released.

I’m confused (I don’t speak English natively) does that mean that it’s compatible with sub-tables?

1 Like

Yeah exactly. It means tables inside of tables and folders inside of folders.

1 Like

ToTable() doesn’t work

:confused:

What isn’t working specifically? Could you provide your script and/or any reproduction steps?

Edit: might’ve found the issue, think I forgot to return the table. I updated the model. Could you try it out and let me know if it’s fixed?

1 Like

Just doing some benchmarks lol

image

I compared mine and mine SHOULD’ve been less optimized and worse, but it seems like it depends more on how fast the script loads and which one loads first; That’s good to know lol

By the way, .ToTable() works fine;

I find it hard to go through the module to try and determine how you should fire the functions since you put the functions inside the module table, instead of doing

function module:FunctionName() or function module.FunctionName()

1 Like

Ah yeah, it’s basically the same as doing module:FunctionName(). Using a colon instead of passing the self argument is called syntactic sugar. It’s also how you’d put a function using a colon inside of the table.

It’s the reason
game.GetService(game, 'ServiceName')
is the same as
game:GetService('ServiceName')

1 Like

You can probably steal the variables that I used in objectTables to make your code compatible with all the other values, like RayValue, Color3Value, BrickColorValue, CFrameValue, Vector3Value, etc.

It’s not hard to set up, I would personally do it since it’s simple to add them, as it’s just for example insert things like:

["Color3"] = "Color3Value"; and so on.

Using only the types you allow can be cool for DataStore work since it’s simpler.

You don’t need to have any extra code to support them.

1 Like

I’ll take a look in a little bit.

1 Like

Your module looks cool but I don’t really think it would work with mine and Color3 values as C3’s can’t be saved to the datastore whereas all of the other supported datatypes in my module can. That was really the main point of my module. I could create some more functions like :ToTableForDatastore but I don’t really think that’s necessary for my module. Just wanna keep it minimalistic 'ya know? :stuck_out_tongue:

1 Like

I understand it, I find it a bit weird why you wouldn’t support but I guess people were to use it for datastore work anyways.

Btw I just meant supporting these other datatypes, not migrate them or something lol?

1 Like

I will just leave the little types conversion thing here:

    ["string"] = "StringValue";
	["number"] = "IntValue";
	["boolean"] = "BoolValue";
	["BrickColor"] = "BrickColorValue";
	["Instance"] = "ObjectValue";
	["Vector3"] = "Vector3Value";
	["CFrame"] = "CFrameValue";
	["Color3"] = "Color3Value";
	["Ray"] = "RayValue";

If anyone needs support for these, you guys can replace this part of the code:

image

With that instead, and now it works.

1 Like

Yeah, it won’t be able to be saved to a datastore though unfortunately.

1 Like

Hey everyone!

I created a second, more advanced version of this module that accepts more data types such as Vector3, CFrame and even rays.

Thanks to @LucasTutoriaisSaimo for writing out the datatypes. Unfortunately, it does not support Instance/ObjectValues as that would be beyond the complexity of what this module tries to achieve.

Here’s a full list of what is accepted:
Screen Shot 2021-06-07 at 10.21.39 PM

I’ve edited the original post to include this second, advanced option.

Thanks for reading! :smile:

4 Likes

Great stuff! Now I can make my scripting SO SO much more easier!!

1 Like