Datastore not finishing before game closes

local function Save(Player)
	warn("Saving Trees")
	local TreeDataStore = {}
	
	local WorkspaceTrees = workspace.WorkspaceTrees
	for i,v in pairs(WorkspaceTrees:GetChildren()) do
		local Position = v.PrimaryPart.Position
		local Nums = v:FindFirstChild("Values"):FindFirstChild("Nums")
		
		local Tree = {
			Type = v.Name,
			Position = {["x"] = Position.X,
				["y"] = Position.Y,
				["z"] = Position.Z
			},
			Items = {
				["Log"] = Nums.Log.Value,
				["Stick"] = Nums.Stick.Value,
				["Sapling"] = Nums.Sapling.Value,
			}
		}

		table.insert(TreeDataStore, Tree)
		
	end
	local EncodedDataSave = HttpService:JSONEncode(TreeDataStore)
	print(EncodedDataSave)
	TreeStore:SetAsync("TreeDataStore",EncodedDataSave)
end

Players.PlayerRemoving:Connect(Save)

This function is supposed to save the trees in the workspace. At times, v will become nil in the middle of the code. I believe this is because the game closes fully before it reaches the end of the function. How can I prevent this from happening?

1 Like

Have you tried running the save function using BindToClose?

2 Likes

Yes, I have. It still stopped in the middle of the function

1 Like

Does this print at the end of the function? If not then is there an error that displays in the output?

1 Like

I figured it out. It was an unrelated issue with folders. Sorry about that

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.