DataStore Sometimes Not Saving?

Hi there,

I have a datastore (based off the ROBLOX example, from after reading a bit apparently that ‘example’ is bad practice.)

I’m considering transitioning into using DataStore2, but I want to see if this is still salvageable and fixable before I do that big change.

I have a datastore issue that is tremendously affecting player’s data. Sometimes the data will save, and sometimes it won’t.

For ex. (I have 600 coins and 4 survivals, I win a round and then have 800 coins and 5 survivals. I will leave, go to a new server, and still have 600 coins and 4 survivals. But then, if I rejoin the newer server, I’ll still have 800 coins and 5 survivals…)

I’ve tried showing the code to multiple friends before coming here and it’s stumped all of us. I don’t know why it’s not saving. Here’s portions of the code that are used for saving/getting data:

SAVING DATA:

local function savePlayerData(playerUserId, player)
	print("Beginning to save "..playerUserId.." DATA! 2")
	if sessionData[playerUserId] then
		print(playerUserId.." HAS SESSION DATA!")
		print(sessionData[playerUserId]['Survivals'])
		local success, err = pcall(function()
			
			
			playerData:UpdateAsync(playerUserId, function(oldValue)
				
				print("RETURNING "..playerUserId.." SESSION DATA, AND SAVED IT!")
				
				return sessionData[playerUserId]
				
			end)
			leaderboardData_S:SetAsync(playerUserId, sessionData[playerUserId]['Survivals'])
		end)
		if not success then
			warn("Cannot save data for player! because "..err)
		end
	end
end

SETTING UP DATA:

local function setupPlayerData(player)

local playerUserId = "Player_" .. player.UserId
local data

	local success, err = pcall(function()
		print("Beginning to grab "..playerUserId.." data!")
		playerData:UpdateAsync(playerUserId, function(playerData)
			data = playerData
		end)
	end)
 
	if success then
		print("Beginning to grab "..playerUserId.." data! [SUCCESS!]")
		if data then
			print("Beginning to grab "..playerUserId.." data! [SUCCESS & DATA FOUND!]")
			-- Data exists for this player
			sessionData[playerUserId] = data
		else
			print("Beginning to grab "..playerUserId.." data! [SUCCESS BUT NO DATA FOUND!]")
			-- Data store is working, but no current data for this player
			sessionData[playerUserId] = {Coins=0, Survivals=0, BonesEnabled=true, Accessories={}, Effects={}, Upgrades={}, Pets={}, Achievements={}, Equipped={}}
		end

game.Players.PlayerAdded:Connect(setupPlayerData) — > fires setupPlayerData
game.Players.PlayerRemoving:Connect(saveOnExit) — > fires saveOnExit which fires savePlayerData

I can provide more if needed, but I have no idea what the problem is. I don’t believe it’s a throttling error because I’m not getting warnings. I used to use SetAsync for saving data (because that’s what the ROBLOX wiki said to do), but after reading up on it, people believe it’s bad practice if you’re constantly updating the data.

So does anyone have better methods? Is this even salvageable? Or do you think I should just implement DataStore2?

This script is heavily based off this tutorial ROBLOX gave, so if you need more context, that tutorial basically the rest of this script (parts I didn’t include.)

Thank you so much for reading,

1 Like

I’m not sure if this would fix your problem or not, but have you binded a function to the BindToClose event?

Here’s how that’d look:

game:BindToClose(function() wait(10) end)

Basically, this will prevent the server from shutting down until 10 seconds have passed; when everybody has left the server. The issue might be if you’re playing in a server by yourself, the server is shutting down too quickly before it can save your data. Either way, you should be using this to prevent shutdowns before the server can save the player’s data. Hope this helps!

8 Likes

Thanks for the reply!

I never had that, just added it now, but the main issue is it’s failing to save even when players simply leave…

But I did add what you said, so thank you for that as well!

1 Like

Those can go hand-and-hand though. This became muscle memory for me when ever I’m writing DataStore systems, it just makes it much more reliable.