Datastores seem to reset sometimes

My datastores keep resetting.

This has happened 2 times now, it doesn’t really matter for now, but when I publish my game, I can’t have my datastores resetting.

I have no idea why this happens.

Script:

local dataStore = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrid = "id_"..plr.UserId
	local save1 = plr.leaderstats.Goals
	local save2 = plr.leaderstats.Wins
	local save3 = plr.leaderstats.Coins
	local save4 = plr.FirstPlay
	
	local GetSaved = dataStore:GetAsync(plrid)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
		save3.Value = GetSaved[3]
		save4.Value = GetSaved[4]
	else
		local NumberForSaving = {save1.Value, save2.Value, save3.Value, save4.Value}
		dataStore:GetAsync(plrid,NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	dataStore:SetAsync("id_"..plr.UserId, {plr.leaderstats.Goals.Value, plr.leaderstats.Wins.Value, plr.leaderstats.Coins.Value, plr.FirstPlay.Value})
end)
1 Like

your code doesn’t really make sense.
First off, why does save1 - save4 have data loaded? When the player joins, shouldn’t those values be 0?
Additionally, the issue is probably with you saving the data. I recommend having a loop that runs every 120 seconds or so and autosaves the data instead of when they leave, as that can cause issues because the values could get removed when the player leaves and its descendants are destroyed.

I’ll try that.

What doesn’t make sense about saving the amount of goals, wins and coins a player has?


You see this piece of code? This doesn’t do anything, did you mean to do setAsync()?