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?