Is this an ok set-up for saving?

Hello, my goal is to save every three minutes, on shutdown and on leave. I have provided an example where the datamodule.saveData(player) function saves data for the player. My questions are the following:

1. Is it ok to save on all of these three conditions (Removal, shutdown, and a timer)? Could this prevent other players’ data from saving if all of these three were to somehow fire at the same time?

2. Is there a better way to get this result in general?

3. Does this actually save data on shutdown, or will some players get neglected?

local function timedSave(Player)
	spawn(function()
		local saveInterval = 180 --  three minutes
		local startInterval = 0
		while game.Players:FindFirstChild(Player.Name) do
			wait(1)
			startInterval = startInterval + 1
			if startInterval == saveInterval then
				startInterval = 0
				dataModule.saveData(Player)
			end
		end
	end)
end

players.PlayerAdded:Connect(function(Player)
	timedSave(Player)
end)

players.PlayerRemoving:Connect(function(player)
	dataModule.saveData(player)
end)

game:BindToClose(function()
	if runService:IsStudio() then
        wait(1)
    else
		for _,player in ipairs(game.Players:GetPlayers()) do
			coroutine.wrap(dataModule.saveData)(player,true)
		end
	end
end)
2 Likes