Please excuse me if the answer to my question is painfully obvious. I am quite new to DataStores and how they work.
I have this script that starts when a player joins the game. Sorry if it's a bit lengthy.
local datastore = game:GetService("DataStoreService")
local ds = datastore:GetDataStore("PlayerData")
game.Players.PlayerAdded:connect(function (plr)
repeat wait() until plr.Character
local stats = Instance.new("Folder",plr)
stats.Name = "stats"
cash = Instance.new("IntValue",stats)
cash.Name = "cash"
diamond = Instance.new("IntValue",stats)
diamond.Name = "diamond"
banned = Instance.new("BoolValue",stats)
banned.Name = "banned"
admin = Instance.new("BoolValue",stats)
admin.Name = "admin"
gold = Instance.new("BoolValue",stats)
gold.Name = "gold"
key = plr.UserId --unique key to each player.
plr.stats:WaitForChild('cash')
local success, error = pcall (function () --pcall to handle errors.
savedData = ds:GetAsync(key) --load the data, if any.
end)
if success and savedData then --load if success.
cash.Value = savedData[1]
diamond.Value = savedData[2]
banned.Value = savedData[3]
admin.Value = savedData[4]
gold.Value = savedData[5]
else
local dataToSave = {
cash.Value,
diamond.Value,
banned.Value,
admin.Value,
gold.Value
}
end
if error then
print("Error with loading the data of: "..plr.Name)
--if can't access DataStore, print error.
end
And this is the segment of code that runs when a player leaves.
game.Players.PlayerRemoving:connect(function ()
local dataToSave = {
cash.Value,
diamond.Value,
banned.Value,
admin.Value,
gold.Value
}
ds:SetAsync(key, dataToSave) --save the values when player leaves.
end)
It works fine, however when a few players join the game at relatively similar times, some player's data do not load. (e.g a player's 'cash' was 400, but fails to load on rejoin, while other players' data does.) Is this an issue with my script, or is the DataStore being accessed too many times at the same time? Otherwise, how could I resolve this?
Any answers or hints would be appreciated!

1 comment
Well, there is a common error in datastores where you can get a request throttled for having too many requests sent. Maybe that's happening?