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 to a datastore?

Asked by 4 years ago

I am tryng to save mutltiple values that I create when the player's enter. I can save a single value just fine but I realized I need a table to store other values to. I can run the script with no error but it prints the default values instead of the one's from my last playing session. Ideas?

--Get The Service's
local replicatedStorage = game:GetService("ReplicatedStorage")
local timePlayedValue = replicatedStorage:WaitForChild("TimePlayedValue")
local totalTimeValue = replicatedStorage:WaitForChild("TotalTimeValue")
local httpsService = game:GetService("HttpService")
local dsService = game:GetService("DataStoreService")
local mainStore = dsService:GetDataStore("MainDataStore")

--create player data
game:GetService("Players").PlayerAdded:Connect(function(plr)

    --create folders

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

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

    local gameplayStats = Instance.new("Folder",leader)
    gameplayStats.Name = "GameplayStats"

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


    --create achievements Values

    CompletedTutorial = Instance.new("BoolValue",achievementsFolder)
    CompletedTutorial.Name = "CompletedTutorialAchievement"
    CompletedTutorial.Value = false


    --create gameplay values

    CompletedMissions = Instance.new("IntValue",gameplayStats)
    CompletedMissions.Name = "CompletedMissions"
    CompletedMissions.Value = 0

    FailedMissions = Instance.new("IntValue",gameplayStats)
    FailedMissions.Name = "FailedMissions"
    FailedMissions.Value = 0

    PreviousMission = Instance.new("StringValue",gameplayStats)
    PreviousMission.Name = "PreviousMission"
    PreviousMission.Value = "No Missions Attempted"

    TimesDied = Instance.new("IntValue",gameplayStats)
    TimesDied.Name = "TimesDied"
    TimesDied.Value = 0

    TrapsAvoided = Instance.new("IntValue",gameplayStats)
    TrapsAvoided.Name = "TrapsAvoided"
    TrapsAvoided.Value = 0

    TrapsFailed = Instance.new("IntValue",gameplayStats)
    TrapsFailed.Name = "TrapsFailed"
    TrapsFailed.Value = 0


    --create player values

    Coins = Instance.new("IntValue",playerStats)
    Coins.Name = "Coins"
    Coins.Value = 0


    local currentDay = os.date("*t").day
    local currentMonth = os.date("*t").month
    local currentYear = os.date("*t").year

    local dateJoined = (currentDay.."/"..currentMonth.."/"..currentYear)

    DateFirstJoined = Instance.new("StringValue",playerStats)
    DateFirstJoined.Name = "DateFirstJoined"
    DateFirstJoined.Value = dateJoined

    Rank = Instance.new("StringValue",playerStats)
    Rank.Name = "Rank"
    Rank.Value = "Junior Assistant"

    totalTime = Instance.new("IntValue",playerStats)
    totalTime.Name = "TotalTimePlayed"
    totalTime.Value = 0

    timePlayed = Instance.new("IntValue",playerStats)
    timePlayed.Name = "TimePlayed"
    timePlayed.Value = 0

    Experience = Instance.new("IntValue",playerStats)
    Experience.Name = "Experience"
    Experience.Value = 0

    --load the data
    local success, err = pcall(function()
        local data = mainStore:GetAsync(plr.UserId) 
        if data then

            --gameplay values
            CompletedMissions.Value = data[1]
            FailedMissions.Value = data[2]
            PreviousMission.Value = data[3]
            TimesDied.Value = data[4]
            TrapsAvoided.Value = data[5]
            TrapsFailed.Value = data[6]

            --player stats value
            Coins.Value = data[7]
            DateFirstJoined.Value = data[8]
            Rank.Value = data[9]
            totalTime.Value = data[10]
            timePlayed.Value = data[11]
            Experience.Value = data[12]

            print(CompletedMissions.Value, FailedMissions.Value, PreviousMission.Value, TimesDied.Value, TrapsAvoided.Value, TrapsFailed.Value, Coins.Value, DateFirstJoined.Value, Rank.Value, totalTime.Value, timePlayed.Value, Experience.Value)

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

--Increment Time Values
incrementor = coroutine.create(function()
    while wait(1) do
        timePlayed.Value = timePlayedValue.Value + 1
        totalTime.Value = totalTimeValue.Value + 1
    end
end)

coroutine.resume(incrementor)

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

    local DataToSave = {CompletedMissions.Value, FailedMissions.Value, PreviousMission.Value, TimesDied.Value, TrapsAvoided.Value, TrapsFailed.Value, 
    Coins.Value, DateFirstJoined.Value, Rank.Value, totalTime.Value, timePlayed.Value, Experience.Value}

    local success, err = pcall(function()
        mainStore:SetAsync(player.UserId,DataToSave)
    end)
    if err then
        print("error saving")
    end
end)


0
Make sure you have STUDIO API enabled uhSaxlra 181 — 4y

1 answer

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

I would use HttpService:JSONEncode and HttpService:JSONDecode. You may need HTTP requests on, but I am not sure. Examples:

print(game:GetService("HttpService"):JSONEncode({"hi","Hello","HOI!"}))
--["hi","Hello","HOI!"]

,

print(game:GetService("HttpService"):JSONDecode'["hi","Hello","HOI!"]')
--table: 0x07cc1b5448ca7e84
--unpacking this table results in 'hi Hello HOI!' but the table is just {"hi","Hello","HOI!"}).

,

print(unpack(game:GetService("HttpService"):JSONDecode'["hi","Hello","HOI!"]'))
--hi Hello HOI!
-- I wouldn't use unpack, I used it here just to show you the values.

. To get the table you could just do

local returndata = '["hi","Hello","HOI!"]'--This would be the data store output.
local table = game:GetService("HttpService"):JSONDecode(returndata)

Happy Scripting!

Ad

Answer this question