How could I save new items?

I’m currently attempting a DataStore system that would save all the items detailed below:

image

image

https://gyazo.com/26c8897ebca20a487aa94dfadb88183f

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?

4 Likes

Saving Multiple Values In Data Store I’ve seen a few posts over this but here’s one I like.

1 Like

I also suggest to put all of your values into a modulescript, as they’re easily exploitable.

1 Like

Could you define how I could do this please? c:

New to the entirety of this.

https://www.roblox.com/library/4805575741/DataManager

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()
3 Likes

module script

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

1 Like

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.

1 Like

Thank you!

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?

Below is how the template appears:

image

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!

I made my layout similar to yours:

Screenshot_2

Here’s the code to put it all in a dictionary:

Here’s what “saveData” will look like after the function call:

Screenshot_3

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 Like

do it the same way almost.

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.

yes that is possible you can save any ‘name’ that you want because they are just strings

This is what the inside of your datastore will look like after saving. (this is just an example not code)

datastore
{ 
  ["PlayerData"..player.UserId] = {
     ["Stats"] = { Attack= 0, Defense = 0, Morality = 0  },
     ["HeldItem"] = "Item_Name",
     ["Level"] = {EXP = 0, MaxExp = 0},
     ["IsRare"] = false
  }
}
1 Like