- What do you want to achieve?
I want to save data without queuing it because there may be an error with saving data for the player and I don’t want that
- What is the issue?
I have a problem with the database, because I know that it can be done better, I use the DataStore2 module and there is an overflow error, I am looking for a way to fix this error and does anyone who has experience with databases have any ideas on how to fix it?
16:18:59.346 DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 127 - Studio
16:18:59.745 saved PlayerData - Server - DataStore2:266
16:18:59.745 Data store PlayerData was not saved as it was not updated. - Server - DataStore2:280
16:18:59.746 saved PlayerData - Server - DataStore2:266
16:18:59.748 Data store PlayerData was not saved as it was not updated. - Server - DataStore2:280
16:18:59.748 saved PlayerData - Server - DataStore2:266
16:18:59.753 Data store PlayerData was not saved as it was not updated. - Server - DataStore2:280
16:18:59.753 saved PlayerData - Server - DataStore2:266
16:18:59.757 Data store PlayerData was not saved as it was not updated. - Server - DataStore2:280
16:18:59.757 saved PlayerData - Server - DataStore2:266
16:18:59.761 Data store PlayerData was not saved as it was not updated. - Server - DataStore2:280
16:18:59.761 saved PlayerData - Server - DataStore2:266
CODE:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local DataStore2 = require(game.ServerScriptService:WaitForChild("DataStore2"))
DataStore2.Combine("PlayerData", "money", "level", "xp", "rockSpawn", "biome", "rocks")
local ROCK_CONFIG = {
["StoneRock"] = {unlockedByDefault = true},
["IronOre"] = {unlockedByDefault = false},
["GoldOre"] = {unlockedByDefault = false},
["EmberCoal"] = {unlockedByDefault = false},
["Solgold Vein"] = {unlockedByDefault = false},
["Grimshard"] = {unlockedByDefault = false}
}
local function getDefaultRocks()
local rocks = {}
for rock, conf in pairs(ROCK_CONFIG) do
rocks[rock] = conf.unlockedByDefault
end
return rocks
end
local DEBOUNCE_TIME = 5
local lastSaveTime = 0
local function safeSave(store, data)
local now = os.time()
if now - lastSaveTime >= DEBOUNCE_TIME then
lastSaveTime = now
store:Set(data)
end
end
local function setupFolders(player)
local playerStore = DataStore2("PlayerData", player)
local defaultData = {
money = 0,
level = 1,
xp = 0,
rockSpawn = 5,
biome = "Grassland",
rocks = getDefaultRocks()
}
local playerData = playerStore:Get(defaultData)
-- leaderstats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = playerData.money
money.Parent = leaderstats
local level = Instance.new("IntValue")
level.Name = "Level"
level.Value = playerData.level
level.Parent = leaderstats
local stats = Instance.new("Folder")
stats.Name = "stats"
stats.Parent = player
local xp = Instance.new("IntValue")
xp.Name = "XP"
xp.Value = playerData.xp
xp.Parent = stats
-- PlotSettings (RockSpawn, Biome)
local plot = Instance.new("Folder")
plot.Name = "PlotSettings"
plot.Parent = player
local rockSpawn = Instance.new("IntValue")
rockSpawn.Name = "RockSpawn"
rockSpawn.Value = playerData.rockSpawn
rockSpawn.Parent = plot
local biome = Instance.new("StringValue")
biome.Name = "Biome"
biome.Value = playerData.biome
biome.Parent = plot
-- Unlocks (Rocks)
local unlocks = Instance.new("Folder")
unlocks.Name = "Unlocks"
unlocks.Parent = player
for rockName, unlocked in pairs(playerData.rocks) do
local bool = Instance.new("BoolValue")
bool.Name = rockName
bool.Value = unlocked
bool.Parent = unlocks
end
playerStore:OnUpdate(function(newData)
money.Value = newData.money
level.Value = newData.level
xp.Value = newData.xp
rockSpawn.Value = newData.rockSpawn
biome.Value = newData.biome
for rockName, unlocked in pairs(newData.rocks) do
local bool = unlocks:FindFirstChild(rockName)
if bool then
bool.Value = unlocked
end
end
end)
local function connectValueChanged(value, key)
value.Changed:Connect(function(newValue)
if playerData[key] ~= newValue then
playerData[key] = newValue
safeSave(playerStore, playerData)
end
end)
end
connectValueChanged(money, "money")
connectValueChanged(level, "level")
connectValueChanged(xp, "xp")
connectValueChanged(rockSpawn, "rockSpawn")
connectValueChanged(biome, "biome")
for _, bool in ipairs(unlocks:GetChildren()) do
if bool:IsA("BoolValue") then
bool.Changed:Connect(function(newValue)
playerData.rocks[bool.Name] = newValue
safeSave(playerStore, playerData)
end)
end
end
player.AncestryChanged:Connect(function(_, parent)
if not parent then
playerStore:Set(playerData)
end
end)
end
local function unlockRock(player, rockName)
if not ROCK_CONFIG[rockName] then return false end
local rocksStore = DataStore2("rocks", player)
local rocks = rocksStore:Get(getDefaultRocks())
if not rocks[rockName] then
rocks[rockName] = true
player.Unlocks[rockName].Value = true
rocksStore:Set(rocks)
return true
end
return false
end
local function isRockUnlocked(player, rockName)
local rocksStore = DataStore2("rocks", player)
local rocks = rocksStore:Get(getDefaultRocks())
return rocks[rockName] or false
end
Players.PlayerRemoving:Connect(function(player)
pcall(function()
DataStore2("money", player):Save()
DataStore2("level", player):Save()
DataStore2("xp", player):Save()
DataStore2("rockSpawn", player):Save()
DataStore2("biome", player):Save()
DataStore2("rocks", player):Save()
end)
end)
Players.PlayerAdded:Connect(setupFolders)
game:BindToClose(function()
for _, player in ipairs(Players:GetPlayers()) do
pcall(function()
DataStore2("money", player):Save()
DataStore2("level", player):Save()
DataStore2("xp", player):Save()
DataStore2("rockSpawn", player):Save()
DataStore2("biome", player):Save()
DataStore2("rocks", player):Save()
end)
end
end)