Data not saving for players

I’ve been using this datastore script for a while but now my players are saying that they are losing their items. No errors are given, what is the problem?

local dss = game:GetService("DataStoreService")
local store = dss:GetDataStore("coins-data-0")

local function load(player)
	local value = player.leaderstats.Coins
	local key= "player_" .. player.UserId  
	local data = store:GetAsync(key)
	
	if data then
		value.Value = data
	else
		value.Value = 0
	end
end

local function save(player)
	local success, error = pcall(function()
		local key = "player_" .. player.UserId
		store:SetAsync(key, player.leaderstats.Coins.Value) 
	end)
	
	if not success then
		warn(error)	
	end
end

game.Players.PlayerAdded:Connect(load)
while true do
	for index, player in pairs(game.Players:GetPlayers()) do
		save(player)
	end
	wait(10)
end

(I know :BindToClose() and PlayerRemoved exist so please do not say anything about that)

Thanks,
RGF

1 Like

Well right now you have a script that autosave every 10 seconds but not when the player leaves the game, if the player decides to leave before the autosave function is called the player will lose their progress, and since you dont want us to talk about PlayerRemoving event or bindtoclose there’s nothing much we can do about it. :confused:

well, there is a way to save, you must specify on which moment the data will be saved while the player is playing.

for example<

recently joined-
picks up 1 coin-

quits-

but you don’t need bindtoclose and player removed

alright

then

-player joins
-picks up 1 coin
-server detects that player picked up coins (automatically save his stuff)
whatever, if something happens while data is being saved, maybe will save or not_

-player quits after a few seconds

Your datastore will save every 10 seconds because you specified and works like a function

but if you added something like

player.leaderstats.Coins:GetPropertyChangedSignal("Value"):Connect(function()

--Do the same thing, save everything

end)

it will save each time a player picks up a coin ( individual)

Saving every 10 seconds especially at the same time for each player will cause the data saving to throttle in queue causing saving failures.
I suggest to create a unique data store for each player and set the auto saving to every 60 seconds. Having a player removing and bindtoclose event is necessary for data saving assuming you did not implement that yet.

You need to use profile service.

Let me warn you, don’t listen to the YouTube tutorials, they’re just scams. Just copy the example code he has, then go on from there. Save player data when they leave by inserting data into the profile.Data table. Just a quick disclaimer, you’ll want to clear the table’s contents before saving more stuff, since that table is the player data, not a newly created table unlike regularly using the default standard data saving method.

Why do players even lose data in the first place?

Well, it’s because of Roblox’s lack of security in data saving. Sometimes the data can just… fail…

This can also root from people server swapping too fast, since if one server is still trying to save their data, another server is trying to load it. It results in an error and gives out no data.

I heard of DataStore2, so why not that?

DataStore2 is old and I guess outdated, since it was made in around 2016 while profile service was made in 2019 I think. Older games do use it still, and it works just fine for them, but it’s always best to use the latest and greatest stuff.