How do I save objects?

Hello! I am doing one of those rarity games. I know how to save data with int and number Val. But, how do I save objects?

2 Likes

Store the object properties then you can just recreate it

How? Do I store it like a table or as an array?

1 Like

Are you asking how to save objects in the Data Store?

Yes I am. Like examples, tycoons, they save buildings and droppers.

You can try using serialization, a good serialization module I was able to find was RBLXSerialize. Basically serialization is when you use known methods and techniques to encode an instance as a string of all its properties, in a compact way, that can be decoded back to the original instance.

If you plan to use this for extreme scenarios like storing large player builds, you may need to compress the string before storing it in the datastore(to reduce space). A good library for compressing strings is Text compression by 1waffle1, optimized by boatbomber. Also, it’s useful to note that this library is for compressing large strings, when compressing really small strings it may end up increasing the output length instead.

Lastly, when saving data in a datastore it’s stored as a JSON-encoded string. Each character within the string you’re saving is 1 byte, so you can calculate the total amount of bytes your data takes with the following code(keep in mind datastores currently can store 2^22 bytes per key):

local data = {Coins = 5, Diamonds = 10, Inventory = {}} --example
--No need to activate HTTP requests for this
local encoded = game.HttpService:JSONEncode(data)
local bytes = encoded:len()

print("Data size:", bytes, "bytes!")

Not looking to store builds. Looking to store objects,

Define object, Do you mean an object in the sense of OOP? Basically a dictionary?

Well i suppose you could loop through the tycoon to check which parts are transparent and which ones arent, then make it so that if they are visible you’d create a bool that was true

i dont really have experience in tycoons

local DataStoreService = game:GetService("DataStore")
local TycoonData = DataStoreService:GetDataStore("TycoonData")

game.Players.PlayerRemoving:Connect(function(player)
local tycoonParts = {}

for i, v in pairs(game.Workspace:FindFirstChild(player.name.." Tycoon"):GetChildren()) do
 if v:IsA("Part") or v:IsA("MeshPart") then
  if v.Transparency == 0
    local visible = Instance.new("BoolValue", leaderstats.TycoonParts)
    visible.Name = v.Name
    visible.Value = true

    table.insert(tycoonParts, visible)
   end
  end
 end

 local success, errormessage = pcall(function()
  TycoonData:SetASync(player.UserId.."-TycoonData", tycoonParts)
 end)

 if success then
  print("Success")
 end
end)

And then when they joined you would loop through a tycoon model or sum and check if the value of the name of the part was true, if its true it would be visible and the pad wouldnt spawn for you to buy it

ok this doesnt make sense

1 Like

Yeah. I meant as dictionaries, sorry for being confusing.

You can only store the properties of the object(basically what separates it from the default object) and recreate it from the properties when you load it from the datastore(by making a function within the object that constructs it from a set of data points). That way you just need to store a dictionary without having to store things such as metatables functions and such.

Basically, it comes down to the question “What’s the smallest and simplest amount of data I can store to reconstruct the object”

For example, if you have an OOP object for pets, you create a function for extracting the info making it unique to the player it belongs to(id, name, rarity, level, exp, etc.), save that to the datastore, and when you load it from the datastore have another function for constructing a pet object using these properties.

You “save” objs by having them in your program as things you can clone.
Or as parts coned and put together. Or put together stored and cloned.
Many things in Roblox are stored at the site and you just call the ID numbers.

So I create a table? Like this?

local dss = game:getservice(“DataStoreService”)
local PartData = dss:GetDataStore(“Part Data”)

local data = {
workspace.part
}

game.Players.PlayerRemoving:connect(function(plr)
       PartData:SetAsync(plr.UserId, plr[data]
end)

You technically cannot save objects, as Roblox’s data doesn’t work that way unfortunately. However, if you have specific names, positions, etc, then you can save those! Let’s say I have five parts for example. My parts are named after their color. My blue part’s name is “Blue”, my red part’s name is “Red”, my yellow part’s name is “Yellow”, my purple part’s name is “Purple”, and my orange part’s name is “Orange”. If I wanted to save all five of those parts, I’d have to save the names of them.

local dss = game:getservice(“DataStoreService”)
local PartData = dss:GetDataStore(“Part Data”)

local data = {
workspace.Purple.Name,
workspace.Blue.Name,
-- And you'd continue from there.
}

game.Players.PlayerRemoving:connect(function(plr)
       PartData:SetAsync(plr.UserId, plr[data]
end)

I used your code because I suck at memorizing how to write data :melting_face:. However, if you know how to write data, change up the script how you need to change it. Just remember to save the names of your objects.

2 Likes

Ah! I see. If I want to save objects then I have to do Name and it will save right?

Yes, that is correct. Data can store everything string related, number related, and so on. Sadly, objects are not supported just yet, so yes, Name will work for you.

1 Like

Hmm. Alright! Thanks for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.