Help With Serialization DataStore2

So basically, how my game works is there is a folder for each player, with their name as the folders name. Inside, there is a folder called SwordsOwned with a bunch of Boolian Values Named after each sword in the game. If it is true, the player owns that item, if it is false they do not.

What I want to do, is save these values so a player keeps their progress with DataStore2. I looked here to try and replicate this into my game, however for some reason it doesn’t work.

How would I go about this?

Edit: To sum up, all I want to know is how you would save the values of multiple boolean Instances values using datastore2, any help much appreciated! :grinning:

DataStore2 works like any other datastore only with extra stuff.

Datastores arent made to save Boolean values as they are so the best way would be to translate the bool into string.

true → “true”

if Value == true then
    return "true"
end

Its also easy to get your original values back too

if Value  == "true" then
    return true
end
1 Like

Thank you I will now try and put this into practise
Edit: But how would you save multiple values into one datastore tho? Like a dictionary with all swords listed and stating if it is true or not, how would you save that into one datastore

A bit like this.

One thing I will suggest is to remove all the values, you have no need for them as a script can do it better.

local Swords = Folder:GetChildren()
local CombinedData = ""
-- You will need to rethink how you store which swords they have.
for i = 1,#Swords do
    if Swords[i].Value == true then
        CombinedData = CombinedData .. "true;"
    end
   if Swords[i].Value == false then
        CombinedData = CombinedData .. "false;"
    end
end
1 Like

A better way of storing owned swords:

local SwordNames = {Sword1,Sword2} -- And so on
local OwnerShipData = {true,false} -- pairs with SwordNames

This means you can easily cut out all the terrible values and still be organised.

With the little bit I gave you you dont really need to have the sword names list but I know some people struggle without it.

Or better yet only store the swords the player owns to cut back on the ammount of data being sent.

1 Like

I guess I just put in boolean values so i can easily check if the player owns the item in other scripts, both on client and server by checking the value.

Ok, but how would I save SwordNames and OwnerShipData into two different datastores? Would I use Save()?

Tried doing this, yet it doesn’t work

    DataStore2.Combine("DATA", "swordNames", "ownerShipData")
	
	local swordNames = {}
	local ownerShipData = {}
		
	for i, v in pairs(ServerStorage.RemoteData[player.Name].SwordsOwned:GetChildren()) do
		table.insert(swordNames, v.Name)
		table.insert(ownerShipData, v.Value)
	end
	
	local swordNamesStore = DataStore2("swordNames", player)
	local ownerShipDataStore = DataStore2("swordNames", player)
	
	swordNamesStore:Save(swordNames)
	ownerShipDataStore:Save(ownerShipData)
	
	for i = 1, 5 do
		print(swordNamesStore[i])
		print(ownerShipDataStore[i])
	end

Edit: All I am asking basically is how you would load and save a dictionary or an array using datastore2

Someone help pls is Im so stuck lol