Sometimes the players data is not saving when the servers shutdown

code:

local function onPlayerRemoving(player)
	local data = serializeData(player)
	local success, errorMessage = pcall(function()
		playerDataStore:SetAsync(player.UserId, data)
	end)

	if not success then
		warn("Failed to save data for player: " .. player.Name .. " - " .. errorMessage)
	end
end

local function saveDataOnShutdown(p)
	local data = serializeData(p)
	local success, errorMessage = pcall(function()
		playerDataStore:SetAsync(p.UserId, data)
	end)

	if not success then
		warn("Failed to save data for player: " .. p.Name .. " - " .. errorMessage)
		-- Можно добавить логику для повторной попытки сохранения
	end
end

local function onServerShutdown()
	for _, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			saveDataOnShutdown(player)
		end)
	end

	-- Ожидание, пока все игроки покинут сервер
	repeat
		task.wait(1)
	until #Players:GetPlayers() == 0
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
game:BindToClose(onServerShutdown)

what’s wrong?
BUT I have a softshutdown system script, maybe it affects this problem?

softshutdowncode:

workspace:SetAttribute("ShuttingDown",false)
local Services={
	["TeleportService"]=game:GetService("TeleportService");
	["PlayersService"]=game:GetService("Players");
	["RunService"]=game:GetService("RunService");
}
local function getServerType()
	if game.PrivateServerId ~= "" then
		if game.PrivateServerOwnerId ~= 0 then
			return "VIPServer"
		else return "ReservedServer"
		end
	else return "StandardServer"
	end
end
local DelayFactor=5
local ServerType=getServerType()
local ServerFactors={
	["ReservedServer"]=function()
		local function TeleportPlayer(P)
			P.Content.Value=true
			Services.TeleportService:Teleport(game.PlaceId,P,{["SoftShutdown"]={true,"Removal"}})
		end
		Services.PlayersService.PlayerAdded:Connect(function(P)
			task.wait(DelayFactor)
			TeleportPlayer(P) DelayFactor/=2
		end)
		for _, P in pairs(Services.PlayersService:GetPlayers()) do 
			TeleportPlayer(P) task.wait(DelayFactor)
			DelayFactor/=2
		end
	end,
	["StandardServer"]=function()
		game:BindToClose(function()
			spawn(function()
				if Services.RunService:IsStudio() then
					warn("Soft Shutdown: System Works Only In Actual Game")
					return
				end
				workspace:SetAttribute("ShuttingDown",true)
				local UITick=os.clock()
				local ReservedCode=Services.TeleportService:ReserveServer(game.PlaceId)
				task.wait(4)
				local function TeleportPlayer(P)
					if (os.clock()-UITick)<0.5 then
						task.wait(1.25)
					end
					P.Content.Value=true
					delay(0.3,function()
						Services.TeleportService:TeleportToPrivateServer(game.PlaceId,ReservedCode,{P},nil,{["SoftShutdown"]={true,"Addition"}})
					end)
				end
				Services.PlayersService.PlayerAdded:Connect(function(P)
					TeleportPlayer(P)
				end)
				for _, P in pairs(Services.PlayersService:GetPlayers()) do 
					TeleportPlayer(P)
				end
			end)
			while #Services.PlayersService:GetPlayers()>0  do
				task.wait()
			end
		end)
	end,
}
Services.PlayersService.PlayerAdded:Connect(function(P)
	local Content=Instance.new("BoolValue")
	Content.Name="Content"
	Content.Parent=P
end)
ServerFactors[ServerType]()

Try using DataStore2, a simple module to save data easily.

There are many tutorials on Youtube. Try to see one.

Sir its not 2019 :wilted_flower:
ProfileStore did implemented it a bit better.
Even tho i don’t like modules not written by myself i do agree that ProfileStore is indeed made well.

Just so you know ipairs/pairs is not needed in Luau and indexing values in table instead of having dirrect referance is bad for perfomance and writing speed overall.

Also please avoid using annonimous functions unless you are creating a closure.

Your code could’ve been a bit simplier

for _, player in ipairs(Players:GetPlayers()) do
	task.spawn(saveDataOnShutdown,Player)
end

Also if you want already made implementation of safe data saving you can use ProfileStore
In your case it was erroring as since you either reached limit or its just a random error there so try doing 4 more attempts to save data.

I also dont understand why are you trying to do that?
That overcomplication of logic and deoptimizzation to me…

The switch table you are making could be made a bit more optimized and easier:

local ServerFactors={
	[1]=function():()--Reserved
		local function TeleportPlayer(P)
			P.Content.Value=true
			Services.TeleportService:Teleport(game.PlaceId,P,{["SoftShutdown"]={true,"Removal"}})
		end
		Services.PlayersService.PlayerAdded:Connect(function(P)
			task.wait(DelayFactor)
			TeleportPlayer(P) DelayFactor/=2
		end)
		for _, P in pairs(Services.PlayersService:GetPlayers()) do 
			TeleportPlayer(P) task.wait(DelayFactor)
			DelayFactor/=2
		end
	end;
	[2]=function()--Standart
		game:BindToClose(function():()
			task.spawn(function()
				if Services.RunService:IsStudio() then
					warn("Soft Shutdown: System Works Only In Actual Game")
					return
				end
				workspace:SetAttribute("ShuttingDown",true)
				local UITick=os.clock()
				local ReservedCode=Services.TeleportService:ReserveServer(game.PlaceId)
				task.wait(4)
				local function TeleportPlayer(P)
					if (os.clock()-UITick)<0.5 then
						task.wait(1.25)
					end
					P.Content.Value=true
					delay(0.3,function()
						Services.TeleportService:TeleportToPrivateServer(game.PlaceId,ReservedCode,{P},nil,{["SoftShutdown"]={true,"Addition"}})
					end)
				end
				Services.PlayersService.PlayerAdded:Connect(function(P)
					TeleportPlayer(P)
				end)
				for _, P in pairs(Services.PlayersService:GetPlayers()) do 
					TeleportPlayer(P)
				end
			end)
			while #Services.PlayersService:GetPlayers()>0  do
				task.wait()
			end
		end)
	end;
[3] = function():()--Default

end;
}



ServerFactors[((game.PrivateServerId~="" and game.PrivateServerOwnerId ~= 0 and 3) or 1) or 2]()

Thanks, I didn’t know about ProfileStore, I will definitely check it out.

1 Like

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