Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Would this work?

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)

1 comment

0

It would not work, because you spelled JSON wrong in line 11.

1 answer

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.