When I join the game with multiple players, the Datastore request added to queue warning keeps showing up on server logs and it takes a few minutes for everyone’s data to load in. Could you guys check my code and help me?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local DataStore2 = require(script:WaitForChild("DataStore2"))
local LevelModule = require(script:WaitForChild("Level"))
local DataVars = {
--General
["Cash"] = {"NumberValue", 100},
["Hunger"] = {"NumberValue", 100},
["Level"] = {"NumberValue", 1},
["XP"] = {"NumberValue", 0},
["CO2"] = {"NumberValue", 0},
--Phone
["PhoneR"] = {"NumberValue", 87},
["PhoneG"] = {"NumberValue", 88},
["PhoneB"] = {"NumberValue", 87},
--Cars
["Mercedes-Benz S Class"] = {"BoolValue", false},
["Rolls-Royce Phantom"] = {"BoolValue", false},
["Honda Civic Hatchback"] = {"BoolValue", false},
["Tesla Cybertruck"] = {"BoolValue", false},
["Tesla Roadster"] = {"BoolValue", false},
["Toyota Prius"] = {"BoolValue", false},
["Hyundai Avante Sedan"] = {"BoolValue", false},
["Lamborghini Aventador SV"] = {"BoolValue", false},
}
function addPlayer(player)
local DataFolder = Instance.new("Folder", player)
DataFolder.Name = "Data"
local CarFolder = Instance.new("Folder", DataFolder)
CarFolder.Name = "Cars"
--Loop to make rep amounts
for key, defaultval in pairs(DataVars) do
wait()
local keystore = DataStore2(key, player)
local RepAmt
if defaultval[1] == "BoolValue" then
RepAmt = Instance.new(defaultval[1], CarFolder)
RepAmt.Name = key
else
RepAmt = Instance.new(defaultval[1], DataFolder)
RepAmt.Name = key
end
local function UpdateRepAmt(updatedValue)
RepAmt.Value = keystore:Get(updatedValue)
end
UpdateRepAmt(defaultval[2])
keystore:OnUpdate(UpdateRepAmt)
print(key.." loaded")
end
end
for _,player in pairs(Players:GetPlayers())do
addPlayer(player)
end
Players.PlayerAdded:connect(addPlayer)
You might want to put a wait() in the for loop for DataVars and CarVars to allow for congestion.
This isn’t the issue, you are most likely surpassing the DataStore:GetAsync() limits, you should always use the DataStore2.Combine method as stated on the page.
Whenever you add new keys always combine it to the master key. This will make all your data save under one big table internally.
Edit:
local LevelModule = require(script:WaitForChild("Level"))
I noticed you also get an infinite yield warning from this line. This could be another cause for your problem. Make sure you are referencing the level module correctly.
Combines all the keys under keysToCombine under the masterKey . Internally, will save all data under those keys into the masterKey as one large dictionary.