Would this Script work?

Hey Devs,
Just a simple question. I made this Script to Save some Stuff of a Folder located in the Player if the Game shutdowns. Im not quite sure if this would work, because I added a table here…
Help would be appreciated!

game:BindToClose(function()
	for i,player in pairs(game.Players:GetPlayers()) do
	local HaloSave = {}
		for i, Halo in pairs(player.Owned:GetChildren()) do
			if Halo then
				table.insert(HaloSave,Halo.Name)
			end
		end
		DataStore:SetAsync("User-"..player.UserId,HaloSave)
	end
end)

Yes, this should work. You can simply have one function to save a player’s data and then call it both when the player leaves, and when the game crashes/closes.
Edit: You can try this and experiment with it by starting a server from studio with 2 clients, and then shutting the game down with the stop button (that should call your BindToClose function).

2 Likes