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

How do I JSONEncode CFrame and Vector3 values?

Asked by
Scriptecx 124
8 years ago

So, I'm testing data storing parts through a table library, and since you can't datastore libraries, im going to have to use JSONEncode and Decode right? Well the library has vector3 values in it and it isn't Encoding them. How would I make it so that JSONEncode includes these values? Thanks!

local DataStore = game:GetService('DataStoreService'):GetDataStore('BaseStore')

Bases = {}
if not DataStore:GetAsync('BaseStore1') then
    print ("Yes")
    local AddPart = function()
        Bases['Part'] = {}
        Bases.Part['Anchored'] = true
        Bases.Part['Size'] = Vector3.new(5, 5, 5)
        Bases.Part['Position'] = CFrame.new (0, 20, 0)
    end

    AddPart()
else 
    Bases = game:GetService('HttpService'):JSONDecode(DataStore:GetAsync('BaseStore1'))
    print (game:GetService('HttpService'):JSONDecode(DataStore:GetAsync('BaseStore1')))
    for i, v in pairs (Bases.Part) do
        print (i)
    end
end

local Part = Instance.new ('Part')
Part.Anchored = Bases.Part.Anchored
Part.Size = Bases.Part.Size
Part.CFrame = Bases.Part.Position
Part.Parent = workspace

print (game:GetService'HttpService':JSONEncode(Bases))
DataStore:SetAsync('BaseStore1', game:GetService'HttpService':JSONEncode(Bases))

1 answer

Log in to vote
2
Answered by
Validark 1580 Snack Break Moderation Voter
8 years ago

I haven't really tested these functions, but try them out:

local HttpService = game:GetService("HttpService")
local function EncodeVector3(Vector3)
    --- Encodes a Vector3 in JSON
    -- @param Vector3 The Vector3 to encode
    -- @return String The string representation in JSON of the Vector3 value.

    return HttpService:JSONEncode{
        Vector3.x;
        Vector3.y;
        Vector3.z;
    }
end

local function DecodeVector3(Data)
    --- decode's a previously encoded Vector3.
    -- @param Data String of JSON, that was encoded.
    -- @return Vector3 if it could be decoded, otherwise, nil

    if Data then
        local DecodedData = HttpService:JSONDecode(Data)
        if DecodedData then
            return Vector3.new(unpack(DecodedData))
        end
    end
    return nil
end

local function EncodeCFrame(CFrameValue)
    --- Encodes a CFrameValue in JSON

    return HttpService:JSONEncode{CFrameValue:components()}
end

local function DecodeCFrame(Data)
    --- decode's a previously encoded CFrameValue.

    if Data then
        local DecodedData = HttpService:JSONDecode(Data)
        if DecodedData then
            return CFrame.new(unpack(DecodedData))
        end
    end
    return nil
end
0
You're missing the parenthesis for the return of the JSONEncode in the EncodeVector3 function. Spongocardo 1991 — 8y
1
@Spongocardo You don't need parenthesis if you are only passing through a table or string Validark 1580 — 8y
0
So I tested it and it works well even without the parenthesis. The more you know, I guess. Spongocardo 1991 — 8y
0
Okay thanks. I havent tried it yet but I looks like it will work. Ill let you know otherwise. Scriptecx 124 — 8y
Ad

Answer this question