A better way to store data?

im making a game right now and i have realized that players with large amounts of cash,
17Qa+ experience alot of dataloss and lag, how can i store my data better??

im currently using this script:

local dataStoreService = game:GetService("DataStoreService")
local leaderstatsDataStore = dataStoreService:GetGlobalDataStore("PlayerStats")

local loaded = {}

game.Players.PlayerAdded:connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	if player.UserId > 0 and player.Parent then
		local leaderstatsData = leaderstatsDataStore:GetAsync(player.UserId)
		if leaderstatsData ~= "Request rejected" then
			if leaderstatsData then
				for i, stat in ipairs(leaderstats:GetChildren()) do
					local value = leaderstatsData[stat.Name]
					if value then
						stat.Value = value
					end
				end
			end
			loaded[player] = true
		end
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		if loaded[player] then
			local leaderstatsData = {}
			for i, stat in ipairs(leaderstats:GetChildren()) do
				leaderstatsData[stat.Name] = stat.Value
			end
			leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData)
		end
	end
	loaded[player] = nil
end)
4 Likes

It’s not due to huge numbers. It’s because you have no pcalls. Make a ModuleScript and implement a retry system in there.

3 Likes

im not sure how to do that, it was just a simple save script

2 Likes

There are a bunch of tutorials on YouTube and Dev Forum, just google it
If you won’t find it, I can make you a simple script

3 Likes

i just need a quick solution i didnt realize it was this advanced

1 Like

If your DataStore is otherwise working correctly then I recommend watching this YouTube tutorial by 5uphi that should help explain to you how to compress the data before storing it into your DataStore: Compress / Optimize Datastore - Roblox Scripting Tutorial

3 Likes

It’s probably the quickest solution out there. You can use DataStore2 if you want to.

1 Like

how do i use pcall in my script?

1 Like

pcall is a function that can catch an unexpected error in the code.

Basic example:

local x = 5
local a = "1"
local success, err = pcall(function()
 x += a
end)
print(success, err)

It will catch the error and return 2 values: if the function execution was successful and the error message (nil if there’s no error)

1 Like

https://devforum.roblox.com/t/save-your-player-data-with-profileservice-datastore-module/667805

ProfileService is quite easy to set up, ive personally never had any issue with it

1 Like

how would that help with storing data?

1 Like

You asked what pcall does. I gave you a basic example.
Basically, instead of doing Asyncs by themselves, wrap them in a pcall

function setAsync(ds:DataStore, key:any, value:any)
 local success, err = pcall(function()
  ds:SetAsync(key, value)
 end)
 if not success then
  warn(err)
  wait(1)
  setAsync(ds, key, value)
 end
end

And then instead of leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData) do setAsync(leaderstatsDataStore, player.UserId, leaderstatsData)

Same goes for getting async.

Edit: Added a delay so that you won’t get rate limited

i kind of get it, but how would that apply to my script? i just add local success, err = pcall(function()
to it?

1 Like

Insert the function somewhere at the beginning of the code and simply replace setAsync with it

replace setAsync with pcall thats all??

game.Players.PlayerRemoving:connect(function(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		if loaded[player] then
			local leaderstatsData = {}
			for i, stat in ipairs(leaderstats:GetChildren()) do
				leaderstatsData[stat.Name] = stat.Value
			end
			task.spawn(function()
				setAsync(leaderstatsDataStore, player.UserId, leaderstatsData)
			end)
		end
	end
	loaded[player] = nil
end)

so i replace

		task.spawn(function()
			setAsync(leaderstatsDataStore, player.UserId, leaderstatsData)

with pcall?

Insert this function. Just replace your old one with this. It’s already done.

I won’t be able to respond no more because it’s midnight

oh okay! thank you! i will try it now

setAsync(leaderstatsDataStore, player.UserId, leaderstatsData)
gives me a red underline, and nothing seemed to change but thanks!