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

Would this work?

Asked by 9 years ago

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)
0
It would not work, because you spelled JSON wrong in line 11. Discern 1007 — 9y

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

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.

Ad

Answer this question