I want to be able to save and load "PlayerDataJSON" with data stores, is there a better way to do this? And will it work?
local PlayerData = { Money = 2500; Plane1 = false, Color1 = "",Color2 = ""; Plane2 = false, Color1 = "",Color2 = ""; Plane3 = false, Color1 = "",Color2 = ""; Plane4 = false, Color1 = "",Color2 = ""; Plane5 = false, Color1 = "",Color2 = ""; } Http = game:GetService("HttpService") PlayerDataJSON = Http:JOSNEncode(PlayerData)
This won't work for a few reasons.
First, table entries can use either ,
OR ;
for separation, so you're constantly overwriting Color1 and Color2 here. You need to use nested tables.
As well, JSON-ifying function are part of the RbxUtility library, not the HTTPService.
You can use this article as reference in the future.
local Utils = assert(LoadLibrary("RbxUtility")) -- The library we need local PlayerData = { Money = 2500; Plane1 = {false, Color1 = "",Color2 = ""}; Plane2 = {false, Color1 = "",Color2 = ""}; Plane3 = {false, Color1 = "",Color2 = ""}; Plane4 = {false, Color1 = "",Color2 = ""}; Plane5 = {false, Color1 = "",Color2 = ""}; } local JSON = Utils.EncodeJSON(PlayerData) -- Convert the table to a string local player = game.Players:GetPlayers()[math.random(game.Players.NumPlayers)] -- Find someone to save the stats to if player then player:WaitForDataReady() -- Make sure it's safe to save first player:SaveString("Stats", JSON) -- Save the stats end
That code uses Data Persistence instead of Data Stores, so I suggest against using it in that form. It also saves to a random player, so you'll have to modify it anyway.