I have a tree system, and when you leave the game, this code is supposed to save those trees, and then load them when you re-enter the game:
local DataStoreService = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local TreeStore = DataStoreService:GetDataStore("TreeStore")
local function Save(Player)
warn("Saving Trees")
local TreeNames = {}
local TreeCFrames = {}
local TreeItemInfo = {}
for i,v in pairs(workspace.WorkspaceTrees:GetChildren()) do
local Position = v.PrimaryPart.Position
local PositionTable = {x = Position.X, y = Position.Y, z = Position.Z}
table.insert(TreeNames,v.Name)
table.insert(TreeCFrames, PositionTable)
local Types = v:FindFirstChild("Values"):FindFirstChild("Types")
local Nums = v:FindFirstChild("Values"):FindFirstChild("Nums")
local InfoTable = {Log = Types.Log.Value, Stick = Types.Stick.Value, Sapling = Types.Sapling.Value, LNum = Nums.Log.Value, StNum = Nums.Stick.Value, SpNum = Nums.Sapling.Value}
table.insert(TreeItemInfo,InfoTable)
end
local TreeCFramesStore = HttpService:JSONEncode(TreeCFrames)
local ItemInfoStore = HttpService:JSONEncode(TreeItemInfo)
warn(TreeNames)
TreeStore:SetAsync("Types",TreeNames)
TreeStore:SetAsync("CFrames",TreeCFramesStore)
TreeStore:SetAsync("ItemInfo",ItemInfoStore)
end
local function Load(Player)
warn("Loading Trees")
local i = 1
local Data1 = TreeStore:GetAsync("Types")
local Data2 = TreeStore:GetAsync("CFrames")
local Data3 = TreeStore:GetAsync("ItemInfo")
local Positions = HttpService:JSONDecode(Data2)
local Info = HttpService:JSONDecode(Data3)
warn(Data1,"<<<<")
if Data1 and Data2 then
print("Starting Tree Loop")
warn(Info)
for _, v in pairs(Data1) do
warn(v)
local Tree = game.ServerStorage.Trees[v]:Clone()
local Types = Tree.Values:FindFirstChild("Types")
local Nums = Tree.Values:FindFirstChild("Nums")
Tree.Parent = workspace.WorkspaceTrees
Tree:SetPrimaryPartCFrame(CFrame.new(Positions[i].x,Positions[i].y,Positions[i].z)) --= Vector3.new(Positions[i])
Types.Log.Value = Info[i].Log
Types.Stick.Value = Info[i].Stick
Types.Sapling.Value = Info[i].Sapling
Nums.Log.Value = Info[i].LNum
Nums.Stick.Value = Info[i].StNum
Nums.Sapling.Value = Info[i].SpNum
i += 1
end
end
end
game.ReplicatedStorage.Data.LoadTrees.Event:Connect(Load)
Players.PlayerRemoving:Connect(Save)
When you chop down a tree, you’re left with a stump, and the name of the model is changed to have Stump at the end. The issue is that when I save the game, it saves the stump, but when I get it back the generate the trees, it’s no longer there and replaced with a regular tree. What is causing this?
When the trees are saved:
When they’re loaded: