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

How would I save data using JSONEncode?

Asked by 4 years ago

I am trying to make a datastore that stores many values and I have been told to use JSONEnocde and Decode. I have the data in a table which the data is gained from replicatedStorage. I then JSONEncode the table and then save the encoded data. I then decode the data using JSONDecode once they enter back in the game and set the values of the replicatedStorage to the table that was decoded. I use a pcall function and get the error when I save and load data. If using this method is inefficent just let me know. Ideas?

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

--Get The Gameplay Stats
local CompletedMissionsValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("CompletedMissionsValue")
local FailedMissionsValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("FailedMissionsValue")
local PreviousMissionValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("PreviousMissionValue")
local TimesDiedValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("TimesDiedValue")
local TrapsAvoidedValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("TrapsAvoidedValue")
local TrapsFailedValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("TrapsFailedValue")

--Get The PlayerStats
local CoinsValue = replicatedStorage.PlayerStats:WaitForChild("Coins")
local DateFirstJoinedValue = replicatedStorage.PlayerStats:WaitForChild("DateFirstJoinedValue")
local RankValue = replicatedStorage.PlayerStats:WaitForChild("Rank")
local ExperienceValue = replicatedStorage.PlayerStats:WaitForChild("XP")

--Table To Store Data
local AllStatsTable = {
    CompletedMissionsValue.Value,FailedMissionsValue.Value,PreviousMissionValue.Value,TimesDiedValue.Value,TrapsAvoidedValue.Value,TrapsFailedValue.Value,
    CoinsValue.Value,DateFirstJoinedValue.Value,RankValue.Value,ExperienceValue.Value,totalTimeValue.Value
}

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

coroutine.resume(incrementor)

game:GetService("Players").PlayerRemoving:Connect(function(player)
    local err,success = pcall(function()
        local jsonTable = httpsService:JSONEncode(AllStatsTable)
        mainStore:SetAsync(player.UserId,jsonTable)
    end)
    if err then
        print("error saving")
    end
end)

game:GetService("Players").PlayerAdded:Connect(function(player)
    local err,success = pcall(function()
        local jsonTable = mainStore:GetAsync(player.UserId)
        local data = httpsService:JSONDecode(jsonTable) 
        if data then
            print("data avaliable")
            data[1] = AllStatsTable[1]
        end

    end)
    if err then
        print("error loading")
    end
end)
0
What's the issue? hiimgoodpack 2009 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

You don't need to use JSON encoding to use datastores, they do that automatically when you read/write to them. All you need is mainStore:GetAsync(key) and mainStore:SetAsync(key, value)

Ad

Answer this question