Bind to close not working properly

So I recently reviewed my script that handles server shutdowns and noticed it was only working when the server closed. The close functions came from youtuber Gnomecode and his datastore video. When the game shutdown, it closes in less than 20 seconds then calls the function.

I have tried removing the modules but that did not work.

--- DataStores
local DS = game:GetService("DataStoreService")
local KnifeDuplicatesData = DS:GetDataStore("KnifeDuplicates")
local DataStore = DS:GetDataStore("Money")
local VehicleDataStore = DS:GetDataStore("Vehicles")
local SkillsData = DS:GetDataStore("Skills")
local KnivesData = DS:GetDataStore("Knives")
local EffectData = DS:GetDataStore("KnifeEffects")
local GarageItemDataStore = DS:GetDataStore("GarageItems")

--- DataModules
local DuplicateDataSave = require(script.Parent.DuplicateSave.DataStore)
local EffectSave = require(script.Parent.EffectSave.DataStore)
local KnifeSave = require(script.Parent.KniveSave.DataStore)
local VehicleSave = require(script.Parent.VehicleSave.DataStore)
local OtherSave = require(script.OtherSave)

local runService = game:GetService("RunService")


function ShutdownSave(player)
	
	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	DuplicateDataSave.SaveData(player,KnifeDuplicatesData,playerUserId,player.KnifeDuplicates,"Duplicates")
	KnifeSave.SaveData(player,KnivesData,playerUserId,player.Knives,"Knives")
	VehicleSave.SaveData(player,VehicleDataStore,playerUserId,player.Vehicles,"Vehicles")
	OtherSave.SaveMoney(player,DataStore)
	OtherSave.Save(player,SkillsData,playerUserId,player.Skills)
	OtherSave.Save(player,EffectData,playerUserId,player.KnifeEffects)
	OtherSave.SaveVehicleData(player,GarageItemDataStore)
	
	print("Shutdown Save Data")
	

end

function GameClosing(reason)
	if runService:IsStudio() then return end
	
	print("Shutting server down for: ",reason.Name)
	local players = game.Players:GetPlayers()
	
	
	for i, v in ipairs(players) do
	
		task.spawn(function()
			ShutdownSave(v)
		end)
	end
end


game:BindToClose(GameClosing)

Please help me understand what is the problem

  1. The game:BindToClose() only triggers when the server is actually closing/shutting down
  2. The script doesn’t handle other types of disconnections or server events (like server restarts, teleports, or player leaving)
  3. Since it’s spawning tasks during shutdown, there might not be enough time for all the data to save properly before the server closes (20 seconds is the maximum time given)

The script is only dealing with server shutting down, no other events. In his video he had task.spawn() and it worked fine. Video: https://youtu.be/H-cDbjd5-bs?t=1143