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

Problems with saving an array into DataStorage please Help??

Asked by 5 years ago
Edited 5 years ago

Hi how are you, i have problem and is saving Arrays with datastore and for that i look for the subject, and I found that with json could be saved arrays then i create this script.

--// Data Store
local DataStore = game:GetService("DataStoreService")
local Data = DataStore:GetDataStore("TestData4")

--// HTTP Services
local HttpService = game:GetService("HttpService")

--// DataTable
local data = {}

--// JsonDecode
local DecodeData = HttpService:JSONDecode(Data)


--// LoadPlayerData
function LoadData(player)
    pcall(function()
        local success, message = pcall(function()
            GetData = Data:GetAsync("player-"..player.userId)
        end)

        local DataIsloaded = player:WaitForChild("IsDataLoaded")

        --// if sucess then
        if success then

            --// if the player don´t have data
            if not GetData then 
                GetData = {0,0,BrickColor.White(),"","M4","M9","None","None"}
                DecodeData = GetData
                print("Sucessfuly Created Data For "..player.Name)
                --Cash,Exp,MaskColor,MaskPattern,Primary,Secondary,Armor,Equipment
            end

            --// Get Data 
            data[player.userId] = GetData
        end 
    end)
end 

--//SaveData
function SaveData(player,Reason)
    --// JsonEncode
    local EncodeData = HttpService:JSONEncode(DecodeData)

    pcall(function()
        if Reason == "Leaving" then
            Data:SetAsync("player-"..player.userId,EncodeData)
            data[player.userId] = nil
        elseif Reason == "Save" then
            Data:SetAsync("player-"..player.userId,EncodeData)
        end
    end)
    print("Saving Data For "..player.Name)
end

--// When Players enter
game.Players.PlayerAdded:connect(function(player)
    LoadData(player)
end)

--// When players leave
game.Players.PlayerRemoving:connect(function(player)
    SaveData(player,"Leaving")
end)

--// Saves every 60 seconds PlayerData

coroutine.resume(coroutine.create(function()
    while wait(60 ) do
        for a,player in pairs(game.Players:GetChildren()) do
            if data[player.userId] ~= nil then
                print("Time To Save")
                SaveData(player,"Save")
            end
        end
    end
end))

And I Got This Problem on the ouput:

  • Can't parse JSON
  • Stack Begin
  • Script 'ServerScriptService.GameData', Line 27
  • Stack End`

Please Help i Need This urgent and sorry if the JSON code, it´s bad i´m new with that.

Thank you for the Help

1 answer

Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
5 years ago

That's because you're trying to store a BrickColor in the DataStore.

Store its name instead, and then when loading the data, turn it back with BrickColor.new()

0
Thanks it works :) imagYTOP 19 — 5y
Ad

Answer this question