Ad
Log in to vote
0

How to reduce data sent over so it doesn't reach data store budget?

Asked by 9 days ago
Edited 9 days ago

I have a script that loads/saves player data when they join and leave the server, and has an auto-save interval where it saves as well. although in the future, I want to make it so per each player it loads saves like 50 things at once per player?? at the moment it does it one by one but I would like to know how to save it and use minimum data store budget? (this script was originally made by Sodaghost13 but I edited and changed it to work how I would like to).
Module Script (In Server Script Service):

local module = {}
local DataStoreService = game:GetService('DataStoreService')
local InventoryItems = DataStoreService:GetDataStore("InventoryItems")
AUTOSAVE_INTERVAL = 180

game.Players.PlayerAdded:Connect(function(Player)
    local InvData = Player:WaitForChild("Backpack"):WaitForChild("Inventory")
    local Bananas = InvData:WaitForChild("Bananas")
    local Apples = InvData:WaitForChild("Apples")
    local Plasters = InvData:WaitForChild("Plasters")
    local Coconuts = InvData:WaitForChild("Coconuts")
    local MetalScraps = InvData:WaitForChild("MetalScraps")
    local Logs = InvData:WaitForChild("Logs")
    local Sticks = InvData:WaitForChild("Sticks")
    local SyncData = {
        SyncBananaStats = InventoryItems:GetAsync('Player-'..Player.UserId..'Bananas'),
        SyncAppleStats = InventoryItems:GetAsync('Player-'..Player.UserId..'Apples'),
        SyncPlasterStats = InventoryItems:GetAsync('Player-'..Player.UserId..'Plasters'),
        SyncCoconutStats = InventoryItems:GetAsync('Player-'..Player.UserId..'Coconuts'),
        SyncMetalScrapStats = InventoryItems:GetAsync('Player-'..Player.UserId..'MetalScraps'),
        SyncLogStats = InventoryItems:GetAsync('Player-'..Player.UserId..'Logs'),
        SyncStickStats = InventoryItems:GetAsync('Player-'..Player.UserId..'Sticks'),
    }
    if SyncData ~= nil then
        local Success, error = pcall(function()
            Bananas.Value = SyncData.SyncBananaStats
            Apples.Value = SyncData.SyncAppleStats
            Plasters.Value = SyncData.SyncPlasterStats
            Coconuts.Value = SyncData.SyncCoconutStats
            MetalScraps.Value = SyncData.SyncMetalScrapStats
            Logs.Value = SyncData.SyncLogStats
            Sticks.Value = SyncData.SyncStickStats
            print("Successful Load")
        end)
        if not Success then
            warn(error)
        end
    end
end)

game.Players.PlayerRemoving:Connect(function(Player)
    local Key = 'Player-'..Player.UserId
    local SyncData = {
        SaveBananas = Player.Backpack.Inventory.Bananas.Value,
        SaveApples = Player.Backpack.Inventory.Apples.Value,
        SavePlasters = Player.Backpack.Inventory.Plasters.Value,
        SaveCoconuts = Player.Backpack.Inventory.Coconuts.Value,
        SaveMetalScraps = Player.Backpack.Inventory.MetalScraps.Value,
        SaveLogs = Player.Backpack.Inventory.Logs.Value,
        SaveSticks = Player.Backpack.Inventory.Sticks.Value,
    }
    InventoryItems:SetAsync(Key..'Bananas', SyncData.SaveBananas)
    InventoryItems:SetAsync(Key..'Apples', SyncData.SaveApples)
    InventoryItems:SetAsync(Key..'Plasters', SyncData.SavePlasters)
    InventoryItems:SetAsync(Key..'Coconuts', SyncData.SaveCoconuts)
    InventoryItems:SetAsync(Key..'MetalScraps', SyncData.SaveMetalScraps)
    InventoryItems:SetAsync(Key..'Logs', SyncData.SaveLogs)
    InventoryItems:SetAsync(Key..'Sticks', SyncData.SaveSticks)
end)

spawn(function()
    while wait(AUTOSAVE_INTERVAL) do
        for i, Player in pairs (game:GetService("Players"):GetChildren()) do
            print("Auto-saving " .. Player.Name .. "'s Data")
            local Key = 'Player-'..Player.UserId
            local SyncData = {
                SaveBananas = Player.Backpack.Inventory.Bananas.Value,
                SaveApples = Player.Backpack.Inventory.Apples.Value,
                SavePlasters = Player.Backpack.Inventory.Plasters.Value,
                SaveCoconuts = Player.Backpack.Inventory.Coconuts.Value,
                SaveMetalScraps = Player.Backpack.Inventory.MetalScraps.Value,
                SaveLogs = Player.Backpack.Inventory.Logs.Value,
                SaveSticks = Player.Backpack.Inventory.Sticks.Value,
            }
            InventoryItems:SetAsync(Key..'Bananas', SyncData.SaveBananas)
            InventoryItems:SetAsync(Key..'Apples', SyncData.SaveApples)
            InventoryItems:SetAsync(Key..'Plasters', SyncData.SavePlasters)
            InventoryItems:SetAsync(Key..'Coconuts', SyncData.SaveCoconuts)
            InventoryItems:SetAsync(Key..'MetalScraps', SyncData.SaveMetalScraps)
            InventoryItems:SetAsync(Key..'Logs', SyncData.SaveLogs)
            InventoryItems:SetAsync(Key..'Sticks', SyncData.SaveSticks)
            print("Successfully Auto-saved " .. Player.Name .. "'s Data")
        end
    end
end)

return module

Script (In the module script):

require(script.Parent)

If you can help it would be greatly appreciated!

0
You should probably be saving your inventory as a single table instead of through multiple keys. XAXA 1235 — 9d

Hi.

Looks like you're using an ad blocker.

That's fine...

No, it really is.

Just, I mean, we put a lot of work into this site, you know?

It would be really really appreciated if you would turn off your ad blocker for our website...

We tried really hard to make our ads as unobtrusive as possible.

If you really hate ads, would you consider a $2 donation via Patreon?

Here's the link.

We love you. We hope you love us too.

.

..

...

Now back to your regularly scheduled Scripting Helpers....

...

..

.

1 answer

Log in to vote
1
Answered by
2eggnog 914
9 days ago
Edited 9 days ago

Instead of storing each inventory item as its own key, store your SyncData dictionary with the items as keys. This will reduce the number of queries by a factor of 7.

function SaveInventory(Player)
    local Key = 'Player-'..Player.UserId
    local SyncData = {
        SaveBananas = Player.Backpack.Inventory.Bananas.Value,
        SaveApples = Player.Backpack.Inventory.Apples.Value,
        SavePlasters = Player.Backpack.Inventory.Plasters.Value,
        SaveCoconuts = Player.Backpack.Inventory.Coconuts.Value,
        SaveMetalScraps = Player.Backpack.Inventory.MetalScraps.Value,
        SaveLogs = Player.Backpack.Inventory.Logs.Value,
        SaveSticks = Player.Backpack.Inventory.Sticks.Value,
    }
    InventoryItems:SetAsync(Key, SyncData)
end
0
I'm assuming when I do :GetAsync I should do SyncAppleStats = InventoryItems:GetAsync(Key) on each line? abnotaddable 341 — 9d
0
yup 2eggnog 914 — 9d
Ad

Answer this question



Ad