Why is my data only sometimes saving properly?

For context, the output is not displaying any errors or data store warnings.

But my script is failing to save data 90% of the time (it works sometimes?). The goal of the script is to save the warp points that the player has unlocked in a data store and then retrieve those warp points once they rejoin so I can load them in.

--Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

--DataStores
local WarpPointsDataStore = DataStoreService:GetDataStore("WarpPointsData-v1.1")
--This is what the data will look like:
--[[
{
	["Kazoo Park"] = false,
	["The Forest"] = false
}	
]]--

--Variables
local WarpPointsValues = script:WaitForChild("WarpPoints") 
--This ^^^ is a folder with a bunch of bool values named according to locations

Players.PlayerAdded:Connect(function(Player)  
	
	--Clone the WarpPoints values into the player for saving and reference purposes in the future
	local Package = WarpPointsValues:Clone()
	Package.Parent = Player
	
	local Data = {}
	local success, errormsg = pcall(function()
		Data = WarpPointsDataStore:GetAsync(Player.UserId.."-WarpPointsData")
	end)
	print(Data)
	if success then
		if Data ~= nil then
			for i, v in pairs(Package:GetChildren()) do
				v.Value = Data[v.Name]
			end
		else
			for i, v in pairs(Package:GetChildren()) do
				v.Value = false
			end
		end
	else
		warn(errormsg)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local WarpPoints = Player:FindFirstChild("WarpPoints")
	local Data = {}
	if WarpPoints then
		for i, v in pairs(WarpPoints:GetChildren()) do
			Data[v.Name] = v.Value 
		end
		local success, errormsg = pcall(function()
			print(Data)
			WarpPointsDataStore:SetAsync(Player.UserId.."-WarpPointsData", Data)
		end)
		if not success then
			warn(errormsg)
		end
	end
end)
1 Like

While the server is closing, sometimes playerRemoving will not fire because of errors, shutdowns, etc. To ensure the data is saved when server shuts down, you can use game:BindToClose() and save the data for each player left in the server

game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
-- save player data
end
end) 
1 Like

This didn’t fix the issue… I’ve stored data for my other games with game:BindToClose before so I am familiar with what it does and how to use it but there seems to be a bug somewhere else that I’m not finding.

The DataStore works perfectly, I tested it several more times (the script was the exact same as the original you posted except with BindToClose) with success every time.


Maybe the system for loading data into the game broke somewhere? It’s definitely not DataStore or I missed something.

Use profileservice instead of regular datastore.

That’s strange. I think I found out what my issue is since this still isn’t working – anytime I set one of the values to false :SetAsync doesn’t even do anything - the weird thing is it’s wrapped in a pcall and I’m not getting any error or success messages despite it clearly erroring at that point.

I set all the values to true and it saved just fine though.

Nevermind, that was only the case once - it just no longer saves the data. Any attempts to print past that point don’t output anything.

Final update here - when I hit ‘stop’ in roblox studio or leave the game in roblox studio’s testing it won’t save the data - as a matter of fact, there is never even a disconnect from server message. But when I do it in-game or in the server testing section of studio it works perfectly fine for some reason. Thanks for helping anyways, not sure why this is happening!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.