Saving and retrieving a table with datastore

I’m new to data store and for my game I need to create a datastore that saves a folder full of values and loads that data when the player rejoins. How would I do so?

1 Like

Simple searches can get you these helpful results :slightly_smiling_face:

1 Like

A really simple script to achieving this. (This is just an example with “Coins” and “Experience”)

local DSS = game:GetService("DataStoreService");
local mainData = DSS:GetDataStore("GameData1");

game.Players.PlayerAdded:Connect(function(player) -- fires when a player joins
local data = mainData:GetAsync(player.UserId) or false;
local datafolder = Instance.new("Folder", player)
datafolder.Name = "PlayerData"
local coins = Instance.new("NumberValue", datafolder)
coins.Name = "PlayerCoins"
local experience = Instance.new("NumberValue", datafolder)
experience.Name = "PlayerExperience"
coins.Value = 50
experience.Value = 0
if data then -- finds if the player even has saved data
local Stats = data.Statistics
coins.Value = Stats.Coins
experience.Value = Stats.Experience
else return;
end
end)

game.Players.PlayerRemoving:Connect(function(player)
local playerDataTable = {}
playerDataTable.Statistics = {
Coins = player:FindFirstChild("PlayerData").PlayerCoins.Value
Experience = player:FindFirstChild("PlayerData").PlayerExperience.Value
}
local success, err = pcall(function()
mainData:SetAsync(player.UserId, playerDataTable)
end)
if not success then
warn("Unable to save " .. player.Name .. "'s data. Error: " .. err)
end
end)

The main issue with this is that in the folder I’d be saving would have many more than just a few things. This also is an issue for what @abrah_m posted. This would work, but how would I compare the data being loaded to a dictionary?

What do you mean “compare the data being loaded to a dictionary”?

In this post:

It saves a table and loads it but I need to know how to manipulate the code while loading it to load it according to my dictionary.

For example: it would load my folder and this folder might have some Int values. The table would load the values of the int values but not the names, I’d need the dictionary to name the values.

So to be exact, you also want to save the objects in the folder’s names and load their names?

No, I’m saving an int value’s value and then comparing that value that is put onto a table to a dictionary, that dictionary compares the value and gets the name from it, for example I’d save an int value with the value of 1. It would load the value only onto a table which would be compared to a dictionary to get its name.