Help In Serializing Item Data

As the title states, I need some help in serializing item data. One of the problems I have with trying to serialize an inventory of items is that the custom datastore I dont know how to really get started. I have experience in serializing before (Mostly with a few properties like Vector3.new or Color3,fromRGB but I don’t really know how to do that for tools with multiple different folders that are going to be in them. This is my current module that is supposed to handle this:

local ServerModules = game.ServerStorage.ServerModules
local ConverterModule = require(ServerModules.Data.ConverterModule)

local DataHandler = {}

function NewData(Player,Data)
	Data.Value.PlayerData.Alive = 1
	--Set New Data
	
end

function LoadData(Player,Data)
	--Decode Items

end

function DataHandler.SetData(Player,Data)
	if Data.Value.PlayerData.Alive == 0 then
		NewData(Player,Data)
	else
		LoadData(Player,Data)
	end
end

function DataHandler.SaveData(Player,Data)
	--Encode Items

end

return DataHandler```

Any help would be nice(I am using the Converter Table--> Instance Module if that helps)

The best way to save things like this is not to save the instances, only the data you need to recreate them.
If you have a gun you only need to save the equivalent of “player X has a gun” and give them a gun when the datastore says they have a gun.

2 Likes

Do this, storing data like dimensions, etc. is much more performant than storing whole instances in the datastore too.

The problem with that though is that im trying to modify the individual properties of the item. I already know that I can just get a tools name from the datastore and get the tool that way but what im trying to do is to save the data of the item without needing any external locations like that.

Save the properties along with what they belong to.
If they are in a folder in the tool a normal table can be generated automatically.
As DevComp just said, storing instances will use up lots more datastore space than you actually need

Wait, can you just save the folder(and still get all the values init)? :exploding_head:

You just do something like this

function Serialize(tool)
      local data = {};

      data.Name = tool.Name;

      data.Properties = {};

      for _, prop in tool.Properties:GetChildren() do
          data.Properties[prop.Name] = prop.Value;
      end

     return data;
end
function Deserialize(data)
      local tool = game.ServerStorage.Tools:FindFirstChild(data.Name):Clone(); -- Best to check it exists first though

      for name, value in data.Properties do
            tool.Properties[name].Value = value;
      end

      return tool;
end
1 Like

Oh I got it working now thanks for the help

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