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

Why isn't my table getting JSON encoded when the player leaves?

Asked by 4 years ago
Edited 4 years ago

I've looked at a bunch of other scripts using JSON to encode/decode tables to save, I did what they did, changing only the names of things, yet when I try to load in my data, it says 'Cannot parse JSON'. Is there an issue with my saving? Any help would be greatly appreciated.

Edit: Entire code added

geldSave = game:GetService('DataStoreService'):GetDataStore('geldSave')
chibiLoad = game:GetService('DataStoreService'):GetDataStore('chibiLoad')

game.Players.PlayerAdded:Connect(function(plr)
    wait(1)
    local geld = Instance.new('IntValue')
    geld.Name = 'ChibiCoins'
    geld.Parent = plr

    local own = Instance.new('Folder')
    own.Name = 'OwnedChibi'
    own.Parent = plr

    local success, err = pcall(function()
        if geldSave then
            local money = geldSave:GetAsync(plr.UserId)
            if money then
                geld.Value = money or 100
            end
        end
        if chibiLoad then
            local chibi = game:GetService('HttpService'):JSONDecode(chibiLoad:GetAsync(plr.UserId))
            for _,v in pairs (chibi:GetChildren()) do
                print(v.Name)
            end
        end
    end)
    if success then
        warn(plr.Name .. ' Data Loaded')
    elseif err then
            warn(err)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local chibis = plr.OwnedChibi:GetChildren()
    local success, err = pcall(function()
        chibiLoad:SetAsync(plr.UserId, game:GetService('HttpService'):JSONEncode(chibis))
        geldSave:SetAsync(plr.UserId, plr.ChibiCoins.Value)
    end)
    if success then
        warn(plr.Name .. ' Saved')
    elseif err then
        warn(err)
    end
end)

game:BindToClose(function()
    local function onRemove(plr)
        local chibis = plr.OwnedChibi:GetChildren()
        local success, err = pcall(function()
            chibiLoad:SetAsync(plr.UserId, game:GetService('HttpService'):JSONEncode(chibis))
            geldSave:SetAsync(plr.UserId, plr.ChibiCoins.Value)
        end)
        if success then
            warn(plr.Name .. ' Saved')
        elseif err then
            warn(err)
        end
    end
    for i,v in pairs(game.Players:GetPlayers()) do
        onRemove(v)
    end
end)
end)
0
Are you sure it's in JSON format? EXAMPLE: {["SomeValue"] = "1"} LikeToScript 108 — 4y
0
The instances in the folder are BoolValues set to true, so I believe so. Unless I'm misunderstanding this: https://www.json.org/json-en.html AkihitoOkami 0 — 4y
0
What's the code bit when you load the data, or maybe what's your whole code? TheBigBro122 427 — 4y
0
I've edited the whole code into it now. AkihitoOkami 0 — 4y

Answer this question