As you can see, throughout the progression of the game - new Pets (Folders) are added into the ‘inStorage’ and I’m looking for a way to save these details - I understand that obviously using in pairs won’t work in this case. Does anyone have any suggestions of how this could be done?
this plugin can handle it all for you, just throw all your value instances into “InitialData” folder.
OR
if you want to do this yourself then it takes a bit of code but its easier then you think.
just save the values like you would any other value. (string/number/bool values only)
EDIT: just remember that every player will have their own folder for data values and these folders will have to be stored on the Server (or else they can be exploited)
LOADING (pseudo code)
--this is the folder that contains all the players' value instances
local dataFolder
--grab data from datastore. (if it doesn't exist then give them the default data folder)
local success,data = pcall(function()
return datastore:GetASync(key)
end)
--loop through the players value instances and apply values that you grabbed from datastore.
local values = dataFolder:GetChildren()
for i, v in pairs(data) do
local valueInstance = values:WaitForChild(i)
valueInstance.Value = v
end
SAVING (pseudo code)
--players folder that contains value instances
local dataFolder
local values = dataFolder:GetChildren()
-- a dictionary that will store all the values
local dictionary = {}
--prepare the dictionary
for i = 1, #values do
dictionary[values[i].Name] = values[i].Value
end
--save the dictionary
local success = pcall(function()
datastore:SetASync(key, dictionary)
end)
--if player LEFT the game then dispose of their folder
dataFolder:Destroy()
local module = {
["ValueNameHere"] = 2; -- could be anything
["StringValueNameHere"] = "Fish"; -- could be anything
}
return module
script:
local module = require(script.ModuleScript) -- just instert where ever your module script is located
local stringvalue = (module["StringValueNameHere"]) -- could be whatever you named it
local intvalue = (module["ValueNameHere"])
print(stringvalue.." ".. intvalue)
This is just an example of using value and sending them to the actual script
Could I work this by creating a ‘Template’ of the Pets stats as shown above and saving the names of the Pets as:
local inStorage = Player["PlayerData"]["RuBeastData"]["inStorage"]
local SavedStorage = {}
for _,i in pairs (inStorage:GetChildren()) do
table.insert(SavedStorage, i)
end
Then saving the corresponding values from within using the methods shown above?
yes, you could save your pet names (or IDs) to a datastore, then next time the player loads you can just compare the names saved with a ‘template’ folder containing the pets etc.
then give the player any pets/stats based on what names (or IDs) matched.
this is how it will work for your inventory as well.
I’ve gotten to the point where I’m able to save the Names of the Owned pets within the Players ‘inStorage’ - Do you have any tips of how I could further this to allow the saving of the corresponding Values?
I’d use a recursive function to save everything inside of a dictionary. The good thing about it is you don’t need to constantly change your code, assuming you keep your layout convention; in other words, it will save any folder size!
Here’s what “saveData” will look like after the function call:
Because this is a dictionary, make sure you use JSONEncode the table before you save it to a datastore!
I hope this example helps. Of course I won’t be handing over the exact code for you to implement, that’s for you to do yourself; that’s the beauty of learning! If you have questions, let me know.
1). Basically you’ll have an “OnlinePlayers” folder in your Server.
2). You’ll have a template folder containing all default data.
3). When a NEW player joins the game you’ll give them the template folder (and place it inside of OnlinePlayers) without changing the values.
4). when an OLD player joins the game, you’ll give them the template folder (and place it within OnlinePlayers) and then you’ll grab all their data from the datastore, and change all the template values to match the datastore values.
5). when you save the player you’ll just load all their values into a table and save the table to the their datastore.
6). when the player leaves you’ll save using the same method AND dispose of their datafolder (from OnlinePlayers)
As seen here, is there anyway of saving the name of the Folder and saving the names of the Values and the Value itself then returning to their corresponding.