Serializing Data to put in data stores

I’m making a simulator and I have used folders for storing the data for each blower.
image Like this.
I need to serialize the equip and Purchased data into a datastores. I’m not sure how to do that. I cant find any relevant posts on the dev forum and I’m stuck. Can someone show me the guideline for doing something like this? Thanks

1 Like

Sorry, serialize what part of it? If they’re just basic data types (non-objects) then make a multi-dimensional table and store that in a key.

There are a few different blowers and I need to save the bool values that states if they are purchased and equip. I need to cycle through all of the blowers and save each blower’s equip and purchased value in the data store.

You would probably just use nested tables for these. You have one table for all of the blowers, and you make multiple other tables within that one for its properties. If you need me to show you an example, I can.

1 Like

Another solution (which I use all the time) is to have them in an order. For example:

{
Blower = 3 -- third blower
}

And then have a value called Order with the number, iterate through all of the blowers and find which one has the order.

Should I make a separate data store for each blower?

No, you can do it all inside of one. The actual object in the datastore would be a table for all of the blowers, which inside that main table would be a separate table for each different blower

Edit: Just to clarify, do you need to store all of the the values, or just the “Equipt” and “Purchased” values?

1 Like

Well if you look at it in the least complicated way, the solution is quite simple.

If possible, you can serialize data as the following…

local Inventory = {
    {
        ItemName = "ItemName",

        Stats = {
               ValueName = Value,
            },
    },
}
1 Like

Just the equip and purchased need to be saved

And one last question before I show my way, how are you storing the items in the player? Is it layed out the same way in the screenshot above (minus all the other values)?

Yes there are just folders with all of the blowers values in them.

Alright, so the way I would do this is by looping through what the player had in their inventory.

local Players = game:GetService("Players")
local ds = game:GetService("DataStoreService")
local itemStore = ds:GetDataStore("Items_1")

--
Players.PlayerAdded:Connect(function(plr)
	local blowers = Instance.new("Folder", plr)
	blowers.Name = "Blowers"

	local data = itemStore:GetAsync(plr.UserId)
	if data ~= nil then
		for _, tbl in pairs(data) do
			local new = Instance.new("Folder", blowers)
			for i, val in pairs(tbl) do
				local new2 = Instance.new("BoolValue", new)
				new2.Value = val
				if i == 1 then
					new2.Name = "Equipt"
				elseif i == 2 then
					new2.Name = "Purchased"
				else
					new.Name = val
				end
			end
		end
	end
end)

Players.PlayerRemoving:Connect(function(plr)
	local tableToStore = {}
	for _, blower in pairs(plr.Blowers:GetChildren()) do
		table.insert(tableToStore, {blower.Equipt.Value, blower.Purchased.Value, blower.Name})
	end
	itemStore:SetAsync(plr.UserId, tableToStore)
end)

When the player leaves, it makes a placeholder table to be stored and checks their “Blowers” folder for any children. From there, it takes the ‘Equipt’ and ‘Purchased’ values, along with the blower’s name, and stores it in a table. It then SetsAsync for the player, storing the placeholder table. When the player joins, if that placeholder table exists, it runs through it and creates a new folder for each blower and both BoolValues ‘Equipt’ and ‘Purchased’ with their respected values. The third value in the table, which is the blower’s name, is used to update the folder’s name.

I just tested this myself, and it worked for me. There are (of course) many other ways of doing it, but this is how I would do it. I could even use more tables inside of other tables, but to keep it simple, I just used one at max.

Since the blowers folder is replicated in to the player when they join instead of making new values could I change the previous values? If that makes sense

Oh i see, so you actually just duplicate the blowers folder into the player?

Yea and that makes it a clean slate. I was thinking once the folder is in the player, changing the values in each blower to be correct.

I can show more of the code if needed

Yeah, if i knew this was how you were doing it, I would’ve made a different script lol.

You would actually use the same system that I made, but instead of creating new objects, you just update the existing object’s values.

local Players = game:GetService("Players")
local ds = game:GetService("DataStoreService")
local itemStore = ds:GetDataStore("Items_1")

--
Players.PlayerAdded:Connect(function(plr)
	local blowers = game.ReplicatedStorage.Blowers:Clone()
	blowers.Parent = plr

	local data = itemStore:GetAsync(plr.UserId)
	if data ~= nil then
		for _, tbl in pairs(data) do
			local getfolder = blowers[tbl[3]]
			if getfolder ~= nil then
				getfolder.Equipt.Value, getfolder.Purchased.Value = tbl[1], tbl[2]
			end
		end
	end
end)

Players.PlayerRemoving:Connect(function(plr)
	local tableToStore = {}
	for _, blower in pairs(plr.Blowers:GetChildren()) do
		table.insert(tableToStore, {blower.Equipt.Value, blower.Purchased.Value, blower.Name})
	end
	itemStore:SetAsync(plr.UserId, tableToStore)
end)

Try this, and see if it works. Lmk if it doesn’t

It doesn’t seem to be working, but it may be on my end. I’ve dissected your code and put it into my script and it doesn’t seem to be working completely.

local DataService = game:GetService("DataStoreService")
local CoinsDataStore = DataService:GetDataStore("PlayerCoins")
local TotalSnowDataStore = DataService:GetDataStore("PlayerTotalSnow")
local itemStore = DataService:GetDataStore("Items_1")

game.Players.PlayerAdded:Connect(function(player)
	wait(0.5)
	local UserId = "player_" .. player.UserId
	local stats = player.leaderstats
	local Coins = stats.Coins
	local TotalSnow = stats.TotalSnow
	local blowers = player.Items.Blowers
	-- load data
	local CoinsData
	local TotalSnowdata
	local succses, errormessage = pcall(function()
		local data = itemStore:GetAsync(player.UserId)
		if data ~= nil then
			if data ~= nil then
				for _, tbl in pairs(data) do
					local getfolder = blowers[tbl[3]]
					if getfolder ~= nil then
						getfolder.Equipt.Value, getfolder.Purchased.Value = tbl[1], tbl[2]
						print(getfolder.Name)
					end
				end
			end
		end
		TotalSnowdata = TotalSnowDataStore:GetAsync(UserId)
		CoinsData = CoinsDataStore:GetAsync(UserId)
		Coins.Value = CoinsData
		TotalSnow.Value = TotalSnowdata
	end)
	if errormessage then
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Coins
	local TotalSnow
	local UserId = "player_" .. player.UserId
	local blowers = player.Items.Blowers
	Coins = player.leaderstats.Coins.Value
	TotalSnow = player.leaderstats.TotalSnow.Value
	local tableToStore = {}
	for _, blower in pairs(player.Items.Blowers:GetChildren()) do
		table.insert(tableToStore, {blower.Equipt.Value, blower.Purchased.Value, blower.Name})
		print(blower)
	end
	itemStore:SetAsync(player.UserId, tableToStore)

	local succsess, errormessage = pcall(function()
		CoinsDataStore:SetAsync(UserId, Coins)
		TotalSnowDataStore:SetAsync(UserId, TotalSnow)
	end)
	if errormessage then
		warn(errormessage)
	end
end)

Here is the full data stores script and Im not sure what is going wrong.
Tell my if its something stupid that I missed when scripting. It may be because I haven’t given the bigger picture of all the things in my game.

Is there any errors in the output?

Nope. None from the Data Stores Script