Values Not Saving to DataStore

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)
1 Like

I believe the problem is the amount of things in table “stats”. Perhaps its too much for the script to do after the player leaves.

Possible. But then my question would be, how do the ‘Big’ games on Roblox (those which clearly need to store even more values than this) code their DataStores? There has to be an effective and reliable way to handle larger DataStores in larger games? “Bee Swarm Simulator”, for example, successfully stores way more values than my script does. How?

1 Like

(Thanks for your previous, quick response, btw :-))

1 Like

Exactly, a effective and reliable way to handle larger data to store, I would advice making it easier for the script, see if theres something you could add to, or remove not so important values to save.

If your going with my advice I recommend making a copy of the original script and apply the changes there.

Hello,
After some more research and working with DataStore I have been told that it is much better to cache data using DataStore2 since it doesn’t put a huge amount of stress on the server which makes it faster and easier to save large amount of data.

Personally haven’t worked with DataStore2.