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

How would I save a table using async's?

Asked by 5 years ago

I am making a datastore that stores a table of mutiple values. I have the player leave with a test table I set and I get no error. When I enter back in the game I get the message saying "no data found" when I clearly used setasync with a table. What am I doing wrong?

local DSS =  game:GetService("DataStoreService")
local MainStore = DSS:GetDataStore("MainStore")
local DataTable = {}
local defaultTable = {0,0,0,false,false}

game:GetService("Players").PlayerAdded:Connect(function(player)

    -- create folders
    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = "leaderstats"

    local PlayerStats = Instance.new("Folder",leaderstats)
    PlayerStats.Name = "PlayerStats"

    local Achievements = Instance.new("Folder",leaderstats)
    Achievements.Name = "Achievements"

    --create player stats
    local Rank = Instance.new("IntValue",PlayerStats)
    Rank.Name = "Rank"

    local Experience = Instance.new("IntValue",PlayerStats)
    Experience.Name = "Experience"

    local Coins = Instance.new("IntValue",PlayerStats)
    Coins.Name = "Coins"

    local FirstTimePlaying = Instance.new("BoolValue",PlayerStats)
    FirstTimePlaying.Name = "FirstTimePlaying"

    --create achievements
    local CompletedTutorial = Instance.new("BoolValue",Achievements)
    CompletedTutorial.Name = "CompletedTutorial"

    --generate the key
    local key = "player-"..player.UserId

    --load the data from the table
    local success,err = pcall(function()
        local data = MainStore:GetAsync(key)
        if data then
            DataTable[1] = data[1]
        else
            DataTable = defaultTable
            print("no data found")
        end
    end)

    if err then
        print("error loading data")
    end

end)

game:GetService("Players").PlayerRemoving:Connect(function(player)

    --generate the key
    local key = "player-"..player.UserId
    DataTable = {6,7,56,true,true}

    --save the data
    local success,err = pcall(function()
        MainStore:SetAsync(key,DataTable)
    end)
    if err then
        print("error saving data")
    end
end)

0
bump jakebball2014 84 — 5y

Answer this question