Table To Object - Any Object-Based Value and functions!

Hello, today I made a module which can transform tables into objects. Or vice-versa.
As you can see in this example:

image

Also as you can see, you can store tables, inside of tables using this module;

image )

You can convert them back to a table, and it keeps the tables inside of tables too.
You could also technically make your own object based table if you know how this works;

Ok! Here’s how to use it:

First of all there’s a variable inside of this module called storage:
This variable is the variable which holds the place on where you wanna store these object-based tables.

image

You can change them which another script by doing:

module.storage = (location)

I recommend doing it on a folder inside of either ReplicatedStorage or ServerStorage :smiley:

If you want you can have only one location, and change it on the module too.
If you have multiple scripts using this system all the time, you might wanna request a module.storage change when you’re calling one of it’s functions.

So let’s go to the functions!

module.CreateObject(table, tableName)

This function is responsible by creating the object-based table;

To use it, do
module.CreateObject(table, tableName)

the “table” variable is the one who holds the table you wanna convert to object-based.
the tableName variable is the name you wanna have for your table. This would be similar to a identifier; When calling a convert object-based table to an actual table you NEED to input this name. Please use a string.

This function will create a folder inside the module.storage variable that you have set up.

module.CreateTable(tableName)

This will return the table back in it’s original table format, and not object-based.
You will need the tableName which you sended before to the module.
If it doesn’t find any object-based table inside of the storage, it will not continue the code and will simply not run.

Now, modules from my understanding do not support “returning” values from their functions to actual scripts, so I had to go around that with a variable inside of this script;

This is not true, I thought it could like that, but no, you IN FACT can return from a Module.
Something about my old modules did not let me return;
The old method is not available anymore; Trying to call it will result in nil.

To get the table value, you would have to do

local data = ds.CreateTable(tableName)
print(data)

the module.Returned variable contains the table, that you requested;
If you requested a table and there was no object-based table, then the script will set that to nil.

This function will return nil if there is no object-based table. So be sure to do:
if data then
everytime you call this function.

Any questions, if I forgot to explain something, please reply to this topic :smiley:

I currently need feed-back. I’m a new developer, and I do not know if someone already made this;

This is the link for the module:

This is open-source and can be modified by you in anyway you want;

Edit: This was my attempt for a “TableValue” in Roblox Studio;

11 Likes

This module is not compatible with functions or object values right now,

What i’m sure they are compatible is with
strings,
numbers / integers,
tables,

and that’s pretty much it;

I might be adding object value support SOON!

but i need to make sure i can detect when it’s an object value, so if you know if you can do

type(v) == “object” please tell me :smiley:

1 Like

Use if v:IsA("ObjectValue") then to check if it’s an ObjectValue

Where did you get that from?? They do support returning, that’s one of their primary uses. If we go by your logic, custom OOP objects wouldn’t be possible.

1 Like

Welp, It didn’t work for me.
Also no, I’m not talking about if v:IsA()
Converting a object based to a table is easier than the other way around.
I need to create a OBJECT value OBJECT when the value on the table is an object value. Not converting a object-based into a table. When doing that, I don’t even need to check. The only thing I need to check is if it is a folder, which that means theres a table, inside of a table.

And also by RETURNING

I mean like this

Main Script:

local data = module.Get("data1")

print(data)

Module script:

module.Get = function()
       return 1
end

I get a nil value no matter what at least on my testing;

The only things I could do in a module script by returning, was returning functions inside of the module, for other functions inside of the same module.
I even saw someone on dev forum complaining about that and saying I would have to make it object-based, and since i coudn’t do that, i made a variable on the module that contains the table.

If that is figured out the other way around, then I will update the script to use the new method.

1 Like

You would do:

local Data = require(module).Get(‘data1’)

This actually does work, here is an example you can try in studio:

local module = {}

module.get = function(x)

return x + 2

end

return module

---Server script
local Module = require(game.ServerScriptService.ModuleScript)

print(Module.get(2))
2 Likes

Hm, just forget about the require() pretend MODULE is a variable inside of the server script which contains require(modulelocatizaiton)

1 Like

Alright, regardless it should work, if you try my example in studio.


https://gyazo.com/ffeb16d4762de7caa3cd5663ebb1932c

3 Likes

Then you might be looking for typeof("userdata"), which are roblox data types. About the module problem, I have absolutely no idea why that happens, definitely not normal behaviour.

2 Likes

his example works, for some reason, mine did not. So… imma update the module LOL

2 Likes

I was thinking here, could it maybe have to do with the fact I have the module inside game.ReplicatedStorage and the script inside game.ServerScriptService?

edit: no, just tested. And YEAH i do not know what happened on my old modules.

2 Likes

You can access ReplicatedStorage from a server script, if not it would throw an error saying it couldn’t find the module.

2 Likes

I tried returning on my module now and it works fine? maybe an issue form the past, idk?

2 Likes

As I said, definitely shouldn’t be happening, now, I think we should close this discussion.

2 Likes
type(object) == "userdata"

typeof() is better for this, as it handles ROBLOX data types gracefully. All Instances will be Instances, and an ObjectValue may only point to an Instance, so checking for typeof(value) == "Instance" will tell you whether it will fit in an ObjectValue.
The reason why you should not check whether type() == userdata, is 1) that ObjectValues expect an Instance, of course, and 2) not all userdata are Instances. You can use newproxy() to get a userdata. TweenInfos are userdata, and are custom ROBLOX data types, but they won’t fit in an ObjectValue.

> Instance.new("ObjectValue").Value = TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.In)
07:12:38.417 - Instance.new("ObjectValue").Value = TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.In):1: invalid argument #3 (Instance expected, got TweenInfo)
07:12:38.418 - Stack Begin
07:12:38.419 - Script 'Instance.new("ObjectValue").Value = TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.In)', Line 1
07:12:38.420 - Stack End

How I would lay it out:

local types = {
	Instance = "ObjectValue",
	string = "StringValue",
	number = ... -- complicated - intvalue? numbervalue?
}

local type = assert(types[typeof(yourvalue)], "Sorry, this type of value is not supported")
local valueobj = Instance.new(type)
3 Likes

I keep number values as intergers since they are basically the same except you have 0.01 you can still have decimals in a int value

1 Like

So I should do

if typeof(v) == “Instance” then
Code
end

Right? You bringed out so many other stuff that I do not understand that I got confused lol :joy_cat:

1 Like

Yeah, basically if if typeof(v) == “Instance”, then it can be put in an ObjectValue, and if it isn’t Instance, then it cannot.

2 Likes

Support for object based values coming tomorrow! I’ll keep you updated! :sparkles:

1 Like

When I was a beginner, I always worked with data using player folders and objects but I had a hard time converting each key value so I just made my own module to convert any type of table/object. Wish I found something like this before but I suppose it was a learning experience for me.

Good work, I didn’t use it but it’s definitely going to help somebody.

2 Likes

I tried saving leaderstats with this, it works, but i wouldn’t really do it because you can’t add new stats and just :sparkles: expect it to work :sparkles:. But if you’re not really gonna change them, you could, it does decrease the size of my code for saving and stuff; But if my game got ever serious I would pass old player info into a new data store method;

1 Like