I’m kind of heartbroken. Been working on a game for over a year, it’s just about ready to release, but … when I test on slow connections I’m now realizing that my ‘leaderboard’ script is very unstable. Sometimes it saves values, other times it simply doesn’t. Given that the script will store purchased items, obviously I can’t release my game if the DataStore is so ‘flaky’.
Like I said, it’s ‘unstable’ - sometimes it writes/saves, sometimes it simply doesn’t.
So … What do I do here? Where’s my code going wrong? How I can I guarantee that values will always be stored, regardless of connection speed?
Here’s the script. Any pointers or tips will be truly appreciated.
local RS = game:GetService("ReplicatedStorage")
local RefreshBackPack = RS.Events:WaitForChild("RefreshBackPack")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MyStats")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local stats = {
"Bolts", "Cogs", "Level", "Nooba",
"LavaRun", "LavaRun02", "LavaRun03",
"ForestRun", "ForestRun02", "ForestRun03",
"DesertRun", "DesertRun02", "DesertRun03",
"IceRun", "IceRun02", "IceRun03",
"JunkYardRun", "JunkYardRun02", "JunkYardRun03",
"ParkRun", "ParkRun02", "ParkRun03",
"BeachRun", "BeachRun02", "BeachRun03",
"JungleRun", "JungleRun02", "JungleRun03",
"FarmYardRun", "FarmYardRun02", "FarmYardRun03",
"PrairieRun", "PrairieRun02", "PrairieRun03",
"StormRun", "StormRun02", "StormRun03",
"HellRun", "HellRun02", "HellRun03",
"ZombieRun", "ZombieRun02", "ZombieRun03",
"BugRun", "BugRun02", "BugRun03",
"SpeedRun", "SpeedRun02", "SpeedRun03",
"LandMineRun", "LandMineRun02", "LandMineRun03",
"RiverRun", "RiverRun02", "RiverRun03",
"SkyRun", "SkyRun02", "SkyRun03",
"DuneRun", "DuneRun02", "DuneRun03",
"CornRun", "CornRun02", "CornRun03",
"GrenadeRun", "GrenadeRun02", "GrenadeRun03",
"QuantumRun", "QuantumRun02", "QuantumRun03",
"SpiderRun", "SpiderRun02", "SpiderRun03",
"DarkRun", "DarkRun02", "DarkRun03",
"Booster", "Booster02",
"CrystalBoost", "SpeedBoost",
"TNT", "MagnetTool", "TPLauncher", "BlowTorch",
"Molotov", "OilCan", "Wrenches", "BeastZapper",
"VisibilityGoggles", "NailGun",
"Kills", "BugKills", "ZombieKills",
"ZomPass", "BugPass", "TelePass",
"LandMinePass", "SpeedPass",
"AddOns", "AddSpoiler", "AddTurret", "AddImmunity",
"AddNooba", "AddBlades", "AddExhaust",
"AddTurretTwo", "AddMegaTurbo", "Difficulty"
}
for _, statName in ipairs(stats) do
local statValue = Instance.new("NumberValue")
statValue.Name = statName
statValue.Value = 0 -- Set an initial value if needed
statValue.Parent = leaderstats
end
local success, data = pcall(function()
return DataStore:GetAsync(tostring(player.UserId))
end)
if success and data then
for statName, statValue in pairs(data) do
local existingStat = leaderstats:FindFirstChild(statName)
if existingStat and existingStat:IsA("NumberValue") then
existingStat.Value = statValue
end
end
end
RefreshBackPack:FireClient(player)
end)
game.Players.PlayerRemoving:Connect(function(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local dataToSave = {}
for _, statValue in pairs(leaderstats:GetChildren()) do
if statValue:IsA("NumberValue") then
dataToSave[statValue.Name] = statValue.Value
end
end
local success, error = pcall(function()
DataStore:SetAsync(tostring(player.UserId), dataToSave)
end)
if not success then
warn("Failed to save data for " .. player.Name .. ": " .. error)
end
end
end)