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

How would you save all values in a folder with datastore?

Asked by 2 years ago

I am creating a find the __ (like find the markers), and I want to create a system where it saves whether or not you have collected a certain marker (im not making find the markers but ill just use it in this post). However, I want to make a system when saving where it looks through a folder and creates a table/dictionary/whateverworks out of the bool values then gives them to roblox's datastore to save/load

I already did most of what I am trying to achieve, but I am having trouble with the saving and loading in part. First off, for my saving, whenever I try and print it, all the values return as nil, and I am unsure why. For the loading, I honestly do not know how to load in the values equal to what they were when you left.

Any help/tips would be appreciated. Right now, I really do not know how I should go about this.

my current code:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    --Create the bool values 
    for _, child in ipairs(game.Workspace.MainZone.SpiderHorses:GetChildren()) do
        local bool = Instance.new("BoolValue")
        bool.Name = child.Name.." Horse"
        bool.Parent = player:WaitForChild("leaderstats")

    end

    local playerUserId = "Player_"..player.UserId


    -- Load Data

    local data
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(playerUserId)
    end)


    if success then 

            --i have no idea how to load in the data from my previous table

        end
        --
end)


game.Players.PlayerRemoving:Connect(function(player)
    local playerUserId = "Player_"..player.UserId

    local data = {}


    -- Saves values that currently exist.

    for _, child in ipairs(player.leaderstats:GetDescendants()) do

        table.insert(data, child.Value)

        end



    local success, errormessage = pcall(function()
        myDataStore:SetAsync(playerUserId, data)
    end)

    if success then 
        print("Data Successful")

    else
        print("Nope")
        warn(errormessage)
    end

end)
0
Just asking are there any errors when you leave the game? If not, were you testing this in studio? Neatwyy 123 — 2y
0
There were no errors, and yes I was testing this in studio. fastaslightning101 2 — 2y

Answer this question