I am trying to store my kills value and rank value in my stats folder but keep getting the error message
(DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 89834601)
I’ve revised your code to make it look a lot cleaner on the eyes.
CODE
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlayerData")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local kills = Instance.new("IntValue", leaderstats)
kills.Name = "Kills"
local rank = Instance.new("StringValue", leaderstats)
rank.Name = "Rank"
local data = nil
local success, err = pcall(function()
data = DataStore:GetAsync(player.UserId.."-data")
end)
if success then
if data then
kills.Value = data
else
-- set a default value when a player first joins
end
else
warn("There was an error whilst grabbing data!")
warn("Error code: ".. err)
end
end)
Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
DataStore:SetAsync(player.UserId.."-data", player.leaderstats.Kills.Value)
end)
if success then
print("Data saved successfully!")
else
warn("There was an error whilst saving data!")
warn("Error code: ".. err)
end
end)
Your old code was quite messy and pretty hard to read. Also, don’t save data if it changes only save data when the player leaves or an auto-save every few minutes.
Adding to that UpdateAsync() would be preferable but I know what to do with it.
When using Instance.new() to create more complicated objects, you should set the parent last as it’s bad practice to set it right at its creation. I didn’t in this example as the objects created are pretty simple and wouldn’t hurt performance at all.