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

Saving the content of a folder?

Asked by 3 years ago

So i'm trying to save the content of a folder that is inside the local player.

This is the script that i made, but the content is not saving. and i have spend well over 3 hours looking for a solution.

Script:

local DataStoraService = game:GetService("DataStoreService")
local DataStore = DataStoraService:GetDataStore("OwnedVehicles")

--// Creating Folder
game.Players.PlayerAdded:Connect(function(Player)
    local Owned_Vehicles = Instance.new("Folder", Player)
    Owned_Vehicles.Name = "Owned_Vehicles"

    --// Getting Conetent of Folder
    local FolderContent = Owned_Vehicles:GetChildren()

    --// Loading Data
    local Data = DataStore:GetAsync(Player.UserId)
    if Data then
        FolderContent = Data.Owned_Vehicles
    end
end)

--// Saving Data
game.Players.PlayerRemoving:Connect(function(Player)
    DataStore:GetAsync(Player.UserId,{
        ["FolderContent"] = Player.Owned_Vehicles
    })
end)

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Heres the issue, You're using :GetChildren() Which returns a table of the childeren of the object, then you are trying to add data from datastore to this server as for saving, you're trying to apply objects from a folder without using :GetChildren(), Which wouldn't work anyway because you cannot save objects in datastore, only basic tables with basic variables.

You will have to write a custom writer to save players vehicles.

For example

local new_data = {}
for vehicle_,vehicle in pairs(Player.Owned_Vehicles:GetChilderen()) do
    local new_vehicle_data = {
        name = vehicle.Name,
    }

    table.insert(new_data, new_vehicle_data)
end

And loading

for vehicle_vehicle in pairs(plr_datastore.data) do
    // Instantiate vehicle stuff likely from prefabs in repstore and apply data to them
end

For each player, this doesn't include get async, set async, looping through players etc, and you'll need to use JSONEncode and JSONDecode to encode/decode stuff to the datastore.

I'll help you on discord if you want. wor#0886

Ad

Answer this question