Ad
Log in to vote
0

How to properly maintain a large, 200+ variable DataStore that saves correctly?

Asked by 3 months ago

Hello,

Problem:My game has 800 BoolValues that needs to be saved. There is a 200 variable per function limit. Therefore, instead of having a single Module script, I have six, each with a unique key. However, since I added additional Module scripts with DataStores, they've stopped saving. The old data from before I had multiple DataStores is still saving, but not the new. I receive no errors, and my game is FilteringEnabled.

Here is how DataStore01 is setup as a Module Script inside ServerScriptStorage:

local module = {}
local DataStoreService = game:GetService("DataStoreService")
local InventoryItems = DataStoreService:GetDataStore("XXXXXX")
local ToolGiverCheck = require(script.ToolGiverCheck)


AUTOSAVE_INTERVAL = 120


function loadToolData01(Player)
    local Key = "PlayerIdNum-"..Player.UserId.."_ToolItems01"
    local DataToGet = InventoryItems:GetAsync(Key)

    local InvData = Player:WaitForChild("Inventory1")
    local HasM9 = InvData:WaitForChild("HasM9")
    --Other variables

    if DataToGet == nil then return end

    HasM9.Value = DataToGet[1] == 1
    --Other variables

print("Data successfully loaded for "..Player.Name)
end

function saveToolData01(Player)  
    local Key = "PlayerIdNum-"..Player.UserId.."_ToolItems01"
local InvData = Player:WaitForChild("Inventory1")
    local HasM9 = InvData:WaitForChild("HasM9")
    --Other variables
local DataToSave = {}

    if HasM9.Value == true then table.insert(DataToSave, 1) else table.insert(DataToSave, 0) end
    --Other variables
InventoryItems:SetAsync(Key, DataToSave)

    print("Data successfully saved for "..Player.Name)
end

game.Players.PlayerAdded:Connect(function(Player) -- Leaderstats and Blue Tools
    local Key = "PlayerIdNum-"..Player.UserId.."_ToolItems01"
    --remove createdata and get data functions
    -- TOOLS
    local tnew = Instance.new

    local ts = tnew("Folder")
    ts.Name = "Inventory1"
    ts.Parent = Player


    local tool1 = tnew("BoolValue")
            tool1.Name = "HasM9"
            tool1.Value = false
            tool1.Parent = ts
--Other variables
loadToolData01(Player)
    repeat wait() until Player.Character
    ToolGiverCheck.giveTools(Player)
end)

-- PLAYER REMOVING

game.Players.PlayerRemoving:Connect(function(Player)
    saveToolData01(Player) 
end)

spawn(function()
    while wait(AUTOSAVE_INTERVAL) do
        for i, Player in pairs (game:GetService("Players"):GetChildren()) do --for every player in the game, save
            print("Auto-saving Blue " .. Player.Name .. "'s Data")
            saveToolData01(Player) 
            print("Successfully Auto-saved " .. Player.Name .. "'s Data")
        end
    end
end)
return module

Unfortunately, despite each Print confirming that the Data loads when the player is added, and saves every 120 seconds, and confirms the save, when the player leaves and returns, the data has not infact saved, despite again received the confirmation that the Data has loaded.

When I only had one DataStore module script it worked fine, I assume I've overloaded this due to having six scripts going at once. What's the workaround if I have so many variables?

Would changing the AutoSave rate help? If they all saved at different intervals? Can I have multiple DataStores hooked up to a single game?

Example: DataStore Script 1

local DataStoreService = game:GetService("DataStoreService")
local InventoryItems = DataStoreService:GetDataStore("DS01")

DataStore Script 2

local DataStoreService = game:GetService("DataStoreService")
local InventoryItems = DataStoreService:GetDataStore("DS02")

I am open to ideas and suggestions. :) Thank you!

0
Check the Server logs when pressing F9, or just add pcall when loading and saving data. Maybe try and extend the seconds of saving, I added mines at 300. Anyways I'm pretty new to Data Stores, so I'm still practicing. Only thing I do is, test multiple times in Studio with 4 players or so, check if any failures occurred in server log I think, and just make sure I added a pcall when load/save . liteImpulse 47 — 3mo
0
=/ I'll try changing the Autosave_Interval and see if it works. No errors in the Server log, in fact it sends the Print Outs that says everything is loading/saving correctly. Never2Humble 63 — 3mo
0
Negative, even when spacing out the Datastore scripts on their Autosave_Interval, it still only saves/loads the first script. =/ Thoughts? Never2Humble 63 — 3mo

Answer this question