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!
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.
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.
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)
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.
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.
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