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. I Get no error but the data does not save. 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)
The biggest problems I see are:
You've got the return values of pcall in the wrong order, it should be local success, err = pcall(
not err, success
You have a coroutine that looks like it's meant to make test changes to your save data, but you never actually update the values in AllStatsTable
. That table's values are set once from the value objects, and never again.
Also, consider pcall-ing the json encode and decode in isolation, so if something throws, you know if it's the json encode/decode or the datastore get/set.