Datastore loading times takes to long

sup guys, found a problem today and need some help.
I have like 40 items stored for each item there is one datastore for “owned” and “equipped”
like “item1_owned” “item1_equipped” now it needs nearly 25 seconds to load all this datastore, and thats to much.
Is there any way to load it faster or a better way to store them?
I thought about a big json in one data so each item would be in there with owned and equipped, would this work out?

Bigger json strings inside one should generally not be any issue, from what i know anyway as ive used that for a while. Alternatively you could make a custom datastore outside roblox but that does open possible vulnerabilities.

Stop using JSON with DataStores, this is done automatically under the hood. Double-encoding is pointless and frankly more tedious to work with when you can simply perform read-write requests and access data tables from DataStore requests directly.

It’s difficult to determine a source for your problem since there’s no code provided here. Most likely your implementation is faulty or running something expensive, because a DataStore request should not be taking a quarter of a minute to perform. Seeing your DataStore implementation would help greatly.

and so on for like 40 times, this takes a while to load

Yeah, that would explain it. You have far too many DataStores operating, which is why your requests are being sent to the queue. You need to get rid of all of this and operate using a single DataStore that saves a table value. This comes with many benefits:

  • Preventing a large number of requests being performed per player

  • Ease of access when wishing to edit data later or run operations with the collected data

  • Easily maintainable due to having only one DataStore

In addition to fixing that, you also need to learn about error handling with pcalls. DataStore requests are web calls and thus requests are sent to the site internally to fetch data from a DataStore. Endpoints can return errors or your data saving may be incorrect, so it’s important to wrap calls with pcall.

Another point to make is that you’ll want to configure your data structure. Depending on what you’re doing, you can consolidate data into certain sets. For example, your equipped stores. If only one tool can be equipped at a time, you can get rid of all of those and just use an indicator as to which is equipped.

Yet another point to make: if you end up having your data sets become too large, look into serialisation and minimalisation techniques by reducing the amount of characters you use. For example, you can represent owned tools as tables and then in those tables, have a value at the first entry that determines the state of the tool, like a 0 for unequipped and a 1 for equipped.

Final point to make: abstract where you can. Make sure to remove as many dependencies from your code where possible and ensure it’s able to scale where needed in the case that you want to start adding more data in the future. It’s worth investing time into reading how to better save data.

These things aside, there are few practice issues in your code. For example, you shouldn’t use the parent argument of Instance.new. There are other tips to be had but most of those come from my own experience on working with DataStores and are full of different concepts and execution styles, such as not relying on the player as a data dependency.

Here’s a template of how you should organise your code:

local Players = game:GetService("Players")
local UserData = game:GetService("DataStoreService"):GetDataStore("UserData")

local function onPlayerAdded(player)
    -- Create folder and items, fetch the data
end

Players.PlayerAdded:Connect(onPlayerAdded)

for _, player in ipairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end

And your DataStore request should be a single one that takes a table value out and runs through it to handle your data set accordingly.

local success, result = pcall(UserData.GetAsync, UserData, player.UserId)
if success then
    if data then
        -- Handle data
    else
        -- Handle no data
    end
else
    -- Handle data request failure, e.g.
    warn(result)
end
2 Likes