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 = {}
for i,v in pairs(workspace.WorkspaceTrees:GetChildren()) do
local Position = v.PrimaryPart.CFrame.Position
table.insert(TreeNames,v.Name)
table.insert(TreeCFrames, CFrame.new(Position))
end
local TreeCFramesStore = HttpService:JSONEncode(TreeCFrames)
TreeStore:SetAsync("Types",TreeNames)
TreeStore:SetAsync("CFrames",TreeCFramesStore)
end
local Data1 = TreeStore:GetAsync("Types")
local Data2 = TreeStore:GetAsync("CFrames")
local function Load(Player)
warn("Loading Trees")
local i = 1
local Positions = HttpService:JSONDecode(Data2)
if Data1 and Data2 then
for _, v in pairs(workspace.WorkspaceTrees:GetChildren()) do
local Tree = v:Clone()
Tree.PrimaryPart.Position = Vector3.new(Positions[i])
i += 1
end
end
end
game.ReplicatedStorage.Data.LoadTrees.Event:Connect(Load)
Players.PlayerRemoving:Connect(Save)
This script is supposed to save the locations of trees in the world, and when the game is loaded up again, the game should generate the trees and put them in their positions. The reason for this is because the trees can be cut down, and new ones can be planted. The tree saving and loading occurs, yet no trees pop up. I checked the explorer and no new trees came in either. How come?
It still wasn’t working. I think this line is the cause of it: for _, v in pairs(workspace.WorkspaceTrees:GetChildren()) do. It’s supposed to put things into that folder, not retrieve things from it. I just need to figure out what I need to loop through
local Data1 = TreeStore:GetAsync("Types")
local Data2 = TreeStore:GetAsync("CFrames")
local function Load(Player)
warn("Loading Trees")
local i = 1
local Positions = HttpService:JSONDecode(Data2)
if Data1 and Data2 then
print("Starting Tree Loop")
for _, v in pairs(Data1) do
print(Positions)
local Tree = game.ServerStorage.Trees[v]:Clone()
Tree.Parent = workspace.WorkspaceTrees
Tree:SetPrimaryPartCFrame(CFrame.new(Positions[i]))
i += 1
end
end
end
I just need to figure out a new bug where the datastore isn’t saving the positions, so the table is empty.
He wanted to know why it wasn’t loading/parenting his tree(s) into the game hence why he marked my post as the solution since I helped him with this issue.
If he is having problems with his datastore he should make a new post regarding that issue since this post is only about the models being parented into the game.
Everything is fixed now. If you want, here’s the finalized code:
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 = {}
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)
print(Position)
end
local TreeCFramesStore = HttpService:JSONEncode(TreeCFrames)
TreeStore:SetAsync("Types",TreeNames)
TreeStore:SetAsync("CFrames",TreeCFramesStore)
print(TreeCFramesStore)
end
local function Load(Player)
warn("Loading Trees")
local i = 1
local Data1 = TreeStore:GetAsync("Types")
local Data2 = TreeStore:GetAsync("CFrames")
local Positions = HttpService:JSONDecode(Data2)
if Data1 and Data2 then
print("Starting Tree Loop")
for _, v in pairs(Data1) do
print(Positions[i])
local Tree = game.ServerStorage.Trees[v]:Clone()
Tree.Parent = workspace.WorkspaceTrees
Tree:SetPrimaryPartCFrame(CFrame.new(Positions[i].x,Positions[i].y,Positions[i].z)) --= Vector3.new(Positions[i])
i += 1
end
end
end
wait(3)
game.ReplicatedStorage.Data.LoadTrees.Event:Connect(Load)
Players.PlayerRemoving:Connect(Save)