DataStore sometimes is loss pls help

Sometimes save data is lost. Is there a way to fix this?

local RunService = game:GetService(“RunService”)
local DataStore = game:GetService(“DataStoreService”)
local Cash1 = DataStore:GetDataStore(“Cash”)

game.Players.PlayerAdded:Connect(function(plr)
local Data = Instance.new(“Folder”, plr)
Data.Name = “Data”
local Cash = Instance.new(“IntValue”, Data)
Cash.Name = “Cash”
Cash.Value = 0

---- Datastore ----
local function FetchData(dataType, defaultValue)
local success, data
repeat
success, data = pcall(function()
return dataType:GetAsync(plr.UserId)
end)
if not success then
warn(“Failed to load data, retrying…”)
wait(1)
end
until success

return data or defaultValue
end
Cash.Value = FetchData(Cash1, Cash.Value)

local loadsave = false

local function saveData()
if not loadsave then
loadsave = true
local function safeSetAsync(dataType, value)
local success
repeat
success = pcall(function()
dataType:SetAsync(plr.UserId, value)
end)
if not success then
warn(“Failed to save data, retrying…”)
wait(1)
end
until success
end
safeSetAsync(Cash1, Cash.Value)
loadsave = false
end
end

local function connectToChangeSignal(property)
property.Changed:Connect(saveData)
end

connectToChangeSignal(Cash)
end)

function PlayerLeavingSave(player)
local DataSaveRemoving = player:FindFirstChild(“Data”)
if DataSaveRemoving then
local function saveData(key, value)
local success
repeat
success = pcall(function()
key:SetAsync(player.UserId, value)
end)
if not success then
warn(“Failed to save data, retrying…”)
wait(1)
end
until success
end
saveData(Cash1, DataSaveRemoving.Cash.Value)
print(player.Name…" Removing Save!")
end
end

game.Players.PlayerRemoving:Connect(PlayerLeavingSave)

function ServerShutdown()
if RunService:IsStudio() then return end
print(“ServerShutdown!”)
for i, player in pairs(game.Players:GetPlayers()) do
task.spawn(function()
PlayerLeavingSave(player)
end)
end
task.wait(5)
end

game:BindToClose(ServerShutdown)

This is what I use for all my datastores. Hope it helps

local datastore = game:GetService("DataStoreService"):GetDataStore("put your own name here") -- the datastore

local playerdata = {}

local function getData(plr) -- gets the data
	local boolval = playerdata[plr.UserId]
	local data
	
	repeat -- repeats until getting the data from the datastore is successful
		local success = pcall(function()
			data = datastore:GetAsync(plr.UserId)
		end)
	until
	success
	
	if data then -- if there is data the bool value will be changed
		boolval.Value = data
	end
end

local function setData(plr) -- sets/saves the data
	local boolval = playerdata[plr.UserId]
	
	repeat -- repeats until the data is successfully saved
		local success = pcall(function()
			datastore:SetAsync(plr.UserId, boolval.Value)
		end)
	until
	success
	
	playerdata[plr.UserId] = nil
end

local function onClosing() -- linked to game:BindToClose()
	for _, plr in pairs(game.Players:GetPlayers()) do -- loops thru all the players
		setData(plr)
	end
end

game.Players.PlayerAdded:Connect(function(plr) -- player joined
	local boolval = Instance.new("BoolValue")
	boolval.Name = "put your own name here"
	boolval.Parent = plr -- you can set it to whatever you want
	
	playerdata[plr.UserId] = boolval
	
	getData(plr)
end)

game.Players.PlayerRemoving:Connect(function(plr) -- player left
	setData(plr)
end)

game:BindToClose(onClosing) -- this fires whenever the server is about to close
1 Like