Saving Multiple String Values in an Already Existing Folder

Hello friends,

I am trying to make the string values, the ones that saves the value of the primary, secondary and melee weapon in a fighting game I am creating, save in a datastore, and I was wondering how I could achieve this.

Basically there is a folder consisting of three values: Primary, Secondary and Melee. They are all string values and they store the value of the weapons in the player’s backpack, and I want to make it, so when they die or rejoin, the value stays the same before they die or rejoin.

Hopefully you understand, and that you can help me!

1 Like

Why not store it in a table instead of a folder?

1 Like

You could just save each string value and use a loop of the saved data to re-apply the corresponding saved values to their respective places, though, you would have to first set it up as a table, as @JustOneFreshAccount has mentioned.

Because the folder is linked to many scripts. This is also one of my first times dealing with data stores

How would I construct this table in a script? When I put this table in the datastore async line, would it save the contents of the table?

Are you trying to save objects through strings? If so, serializing the data would be much better for both performance and the long run!

1 Like

No, I just want the strings to be the same, even after death or leaving

Absolutely, granted you would have to save it as a dictionary rather than an array…

local data = {
    ["Money"] = 157623,
    ["Kills"] = 77672135
}

You could either use a condition-controlled loop to assign each value to the table, but for simplicity, you can directly assign each value, such as:

local data = {
    ["Money"] = leaderstats.Money.Value,
    ["Kills"] = leaderstats.Kills.Value
}

From there, you would save it to the Datastore service as usual.

1 Like

You could also prepare a function such as:

function folderToData(folder:Folder)
local initialData = {}
local children = folder:GetChildren()

for i = 1,#children do
local child = children[i]
if (not child) or (not child.Value) then continue end

initialData[child.Name] = child.Value
end
return initialData
end

In order to “JSONify” the values inside your folder.

2 Likes

Then just save the value of the StringValue, as you can save strings to data stores.

local data = {
   StringValue.Value --// If you have multiple values you need to save those inside a table like this
}

local success, result = pcall(function()
   DataStoreService:UpdateAsync(Key, function()
     return data
   end)
end)
2 Likes

so if i wrote

local data = {
	["Money"] = --StringValue.Value
}

It would save in the table?

1 Like

Could you explain “JSON” for me please?

1 Like

Of course! From there, you can reference the value you’ve appended (added) to the dictionary like this:

data["Money"]
print("This user has this much money:", data["Money"])
1 Like
game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Data = {
			["Primary"] = Character.WeaponSelection.Primary.Value,
			["Secondary"] = Character.WeaponSelection.Secondary.Value,
			["Melee"] = Character.WeaponSelection.Melee.Value
		}
	end)
end)

Is this how i would start making the data storage script?

2 Likes

Yes, although, you would want to create the dictionary when you intend to save the WeaponSelection values to the data store. Creating it now may case trouble, as it won’t keep up to date with any changes done to the WeaponSelection values.

2 Likes

Sorry, I took a two week break to focus on other things, but would I have to create a dictionary or is it not needed in this case.

Also, I want the data storage to save every time the player dies, so when the player dies, the str value would be the same

1 Like

If you’re trying to save multiple pieces of data at once, then yes, you would need to create a dictionary for it. As for saving when the player dies, you can connect your save functions to fire whenever the player’s Humanoid has died.

playerHumanoid.Died:Connect(savefunction)
1 Like
game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Data = {
			["Primary"] = Character.WeaponSelection.Primary.Value,
			["Secondary"] = Character.WeaponSelection.Secondary.Value,
			["Melee"] = Character.WeaponSelection.Melee.Value
		}
	end)
end)

so continuing from this ^^^

How would I save it?
Can you give me an example for the

playerHumanoid.Died:Connect(savefunction)

?

1 Like

I would recommend you to save data using ProfileService out of ease and to prevent data loss. You can find it here: Save your player data with ProfileService! (DataStore Module) - #678 by loleris

The API docs will also provide you with an example of how to save your data, which you can find here: Basic Usage - ProfileService

1 Like

Profile service is kind of hard to understand for beginners. It’s documentation is also confusing. I think suphis data store module is wayyy better and more performant than profile service. Suphis module also provides the same features as profile service such as session locking and many more

Suphi datastore module

1 Like