Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to save a folder using DataStore?

Asked by 5 years ago
local DataStore = game:GetService("DataStoreService"):GetDataStore("ItemStore")
game.Players.PlayerAdded:Connect(function(player)
    local Items = DataStore:GetAsync(player.UserId)
    local Inventory = Instance.new("Folder", player)
    Inventory.Name = "Inventory"
end)

I've been trying to learn how to use DataStores today. Currently, I've made it create a folder for the player. However, I am lost as to how to save the objects or tools inside of the folder and load them again. Would I be storing all of the stuff into a table? And if so, how would you save said table?

Any thoughts?

0
Don't do Instance.new(instance, parent), the 2nd parameter is deprecated and is less efficient than doing Inventory = Instance.new("Folder"); Inventory.Parent = player climethestair 1663 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

You can only store more primitive datatypes in Datastores such as...

Booleans, Numbers, Strings, Tables, ect...

Not things like...

Instances, Vectors, CFrames, ect...

So what you would have to do is come up with a system to convert all the data into a savable format and then when you load it back up again, then have premade objects that you can minorly adjust to the saved data.

For example: In my game people have weapons, and what I do is save which weapon they have as a string and what their weapons stats are in int format. When I load the game up again, I already have a storage of all the weapons in the ServerStorage, so I just clone the weapon based on the string I saved and give it to the player. I then adjust the stats of the weapon I cloned based on the int values I saved.

If you are looking to load say a folder, you could have a structure like this...

function createPlayerFolder(plr, data)
    local folder = premadeFolderOfValues:Clone()

    folder.Stat1.Value = data[1]
    folder.Stat2.Value = data[2]
    folder.Stat3.Value = data[3]

    folder.Parent = whereverYouKeepIt
end

game.Players.PlayerAdded:Connect(function(player)
    local data = getDataFromPlayer() -- However you wanna do it

    createPlayerFolder(player, data)
end)
Ad
Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

You can't save instances in DataStores, but what you can do is save them in an array or dictionary.

Saving: As mentioned before, DataStores can't save instances but you can save their names or values in an array or dictionary. You can loop through all instances in the player's inventory, and add the name of that instance to the table, then proceed with saving

local dss = game:GetService('DataStoreService'):GetDataStore('PlayerData') --retrieving a DataStore by a random name

game.Players.ChildRemoved:Connect(function(player)
    local tbl = {}
    local success, message = pcall(function() --wrapping the saving part in a pcall
        for _,v in pairs(player.Inventory:GetChildren()) do
            tbl[#tbl+1] = v.Name
        end
        dss:SetAsync(player.UserId, tbl) -- saving the data
    end)
    if success then
         print("Saved "..player.Name.."'s data")
    else
        print('Failed to save data: '..message)
    end
end)

Retrieving:

To retrieve the data, we'll have to use GetAsync. When the player joins, we'll call GetAsync if they have any saved data, if they do then we'll loop through the table and insert the instances needed and change its name and or value, else the player has no saved data, and we'll use a blank table.

game.Players.ChildAdded:Connect(function(player)
    local data = dss:GetAsync(player.UserId) or {}
    local inventory = Instance.new('Folder')
    inventory.Name = 'Inventory'
    inventory.Parent = player

    for _, v in pairs(data) do
        local value = Instance.new('StringValue') --You can use whatever instance you want. I'm using a StringValue as an example
        value.Name = v
        value.Parent = inventory
    end
end)

Answer this question