Problems with :BindToClose()

Hello!

I have a problem with my program. I want to detect the closing of the server, and send an http request. For that, I use the BindToClose() function, but I have the impression that my program does not execute until the end.

This is what the data dictionary looks like:

{
			["LastSend"] = os.date("!*t")["day"];
			["Stats"] = {
				["Server"] = {
					["Connections"] = 0;
					["ConnectionsStaff"] = 0;
				};
				["Menu"] = {
					["MenuMain"] = 0;
					["MenuPhone"] = 0;
				};
				["Teams"] = {
					["ST"] = 0;
					["PN"] = 0;
					["GN"] = 0;
					["JT"] = 0;
					["SM"] = 0;
					["CT"] = 0;
				};
				["RolePlay"] = {
					["Calls"] = 0;
					["Braquages"] = 0;
					["Fire"] = 0;
				};
				["Staff"] = {
					["Report"] = 0;
					["Warn"] = 0;
					["Kick"] = 0;
					["Kickwarn"] = 0;
					["Bans"] = 0;
				};
				["Cars"] = {
					["DespawnLeave"] = 0;
					["Spawns"] = 0;
					["Respawns"] = 0;
					["Left"] = 0;
				};
				["Tools"] = {
					["Cones"] = 0;
					["Medical"] = 0;
					["Killed"] = 0;
				};
				["Cash"] = {
					["Generate"] = 0;
					["Sell"] = 0;
					["Taz"] = 0;
				};
			};
		}

In the next loop, I have several values in the dictionary, but only one is print, and the program does not terminate.

local send = {
		["embeds"] = {
			["title"]= "Informations",
			["description"] = "",
			["color"]= 16711680,
		}
	}

	local mess = ""

	for i, v in pairs(data["Stats"]) do

		mess=""

		for e, z in pairs(i) do
			mess=mess.."\n"..e..": "..z
		end

		table.insert(send["embeds"], {
			["title"]= i,
			["description"] = mess,
			["color"]= 16711680,
		})

	end
	
	httpservice:PostAsync("XXX", httpservice:JSONEncode(send))
	datastoreservice:SetAsync("XXX", data)

Someone can help me ?

Where’s the game:BindToClose() method here?

Full code is :

local datastoreservice = game:GetService("DataStoreService"):GetDataStore("XXX")
local httpservice = game:GetService("HttpService")

function reset()
	datastoreservice:SetAsync("XXX", 
		{
			["LastSend"] = os.date("!*t")["day"];
			["Stats"] = {
				["Server"] = {
					["Connections"] = 0;
					["ConnectionsStaff"] = 0;
					["ConnectionsFonda"] = 0;
					["ConnectionsNewPlayers"] = 0;
					["ConnectionsOldPlayers"] = 0;
					["StartedServer"] = 0;
					["ClosedServer"] = 0;
				};
				["Menu"] = {
					["MenuMain"] = 0;
					["MenuPhone"] = 0;
					["MenuGroup"] = 0;
					["MenuSpawners"] = 0;
					["MenuVestiaires"] = 0;
				};
				["Teams"] = {
					["ST"] = 0;
					["PN"] = 0;
					["GN"] = 0;
					["JT"] = 0;
					["SM"] = 0;
					["CT"] = 0;
					["SP"] = 0;
					["VA"] = 0;
					["RATP"] = 0;
				};
				["RolePlay"] = {
					["Calls"] = 0;
					["Braquages"] = 0;
					["Fire"] = 0;
					["KO"] = 0;
					["Arrested"] = 0;
					["Wanted"] = 0;
				};
				["Staff"] = {
					["Report"] = 0;
					["Warn"] = 0;
					["Kick"] = 0;
					["Kickwarn"] = 0;
					["Bans"] = 0;
					["Bant"] = 0;
					["Banp"] = 0;
					["Shutdown"] = 0;
				};
				["Cars"] = {
					["DespawnLeave"] = 0;
					["Spawns"] = 0;
					["Respawns"] = 0;
					["Left"] = 0;
					["Right"] = 0;
					["Lock"] = 0;
					["DespawnReset"] = 0;
					["Warnings"] = 0;
					["Phares"] = 0;
					["Klakson"] = 0;
					["Unlock"] = 0;
				};
				["Tools"] = {
					["Cones"] = 0;
					["Medical"] = 0;
					["Killed"] = 0;
					["Taz"] = 0;
					["Fouille"] = 0;
				};
				["Cash"] = {
					["Generate"] = 0;
					["Sell"] = 0;
					["Taz"] = 0;
					["GetAsync"] = 0;
					["SetAsync"] = 0;
				};
			};
		}
	)
end

function send()
	local data = {
		["content"] = "Statistiques envoyées à "..os.date()
	}
	local code = httpservice:JSONEncode(data)
	httpservice:PostAsync("XXX", code)
end

function add(data)

	for i, v in pairs(game.ServerStorage.Stats:GetChildren()) do
		if data["Stats"][v.Name] ~= nil then
			for _, y in pairs(v:GetChildren()) do
				if data["Stats"][v.Name][y.Name] ~= nil then
					data["Stats"][v.Name][y.Name] = data["Stats"][v.Name][y.Name]+y.Value
				end
			end
		end
	end
	
	local send = {
		["embeds"] = {
			["title"]= "Informations",
			["description"] = "",
			["color"]= 16711680,
		}
	}

	local mess = ""

	for i, v in pairs(data["Stats"]) do

		mess=""

		for e, z in pairs(i) do
			mess=mess.."\n"..e..": "..z
		end

		table.insert(send["embeds"], {
			["title"]= i,
			["description"] = mess,
			["color"]= 16711680,
		})

	end
	
	httpservice:PostAsync("XXX", httpservice:JSONEncode(send))
	datastoreservice:SetAsync("XXX", data)
		
end

game:BindToClose(function()
	local data = datastoreservice:GetAsync("XXX")
	if data == nil then
		reset()
	else
		if data["LastSend"] ~= os.date("!*t")["day"] then
			send()
			reset()
		else
			add(data)
		end
	end
end)