Datastore2 not saving when Server shutsdown

Hello. I am new to DataStore2. While testing on it. I noticed that my data doesn’t save when I shutdown the game. I used AlvinBlox’s datastore2 script. I know how to use BindToClose() function when it comes to the old datastore. I’m not familliar with DataStore2 atm. How would I make it save when a server shutsdown or a player suddenly disconnects without leaving?

local DataStore2 = require(1936396537)

local DefaultGP = 100

game.Players.PlayerAdded:Connect(function(plr)
	
	local InfoDataStore = DataStore2("Info",plr)
	
	local Info = Instance.new("Folder",plr)
	Info.Name = "leaderstats"
	
	local GP = Instance.new("IntValue",Info)
	GP.Name = "GP"
	
	local function GPUpdate(updatedvalue)
		
		GP.Value = InfoDataStore:Get(updatedvalue)
	end
	
	GPUpdate(DefaultGP)
	
	InfoDataStore:OnUpdate(GPUpdate)
end)

game.Workspace:WaitForChild("Part").ClickDetector.MouseClick:Connect(function(player)
	
	local InfoDataStore = DataStore2("Info",player)
	
	InfoDataStore:Increment(5,DefaultGP)
	

end)

I also noticed that the module already has a BindToClose() function but I’m not sure why it’s still not working.

game:BindToClose(function()
		if not fired then
			spawn(function()
				player.Parent = nil -- Forces AncestryChanged to fire and save the data
			end)

			event.Event:wait()
		end

		local value = dataStore:Get(nil, true)

		for _, bindToClose in pairs(dataStore.bindToClose) do
			bindToClose(player, value)
		end
	end)

Please help. Thank you!

1 Like

Maybe add only game:BindToClose() at the end of the script.

I want to, but I don’t know how to do it with DataStore2. That’s why I’m asking for help.